2014-11-06 58 views
0

我想从一个iList中获取名称到另一个名称中,它们都包含相同的ID。C#创建两个iLists之间的关系

public class Notifications 
{ 

    [JsonProperty("note_id")] 
    public int note_id { get; set;} 
    [JsonProperty("sender_id")] 
    public int sender_id { get; set;} 
    public string sender_name { get; set; } 
    [JsonProperty("receiver_id")] 
    public int receiver_id { get; set; } 
    [JsonProperty("document_id")] 
    public int document_id { get; set; } 
    [JsonProperty("search_name")] 
    public string search_name { get; set; } 
    [JsonProperty("unread")] 
    public int unread { get; set; } 
} 
public class CompanyDirectory 
{ 

    [JsonProperty("contact_id")] 
    public int contact_id { get; set; } 
    [JsonProperty("first_name")] 
    public string first_name { get; set; } 
    [JsonProperty("second_name")] 
    public string second_name { get; set; } 
    [JsonProperty("extension")] 
    public string extension { get; set; } 
    [JsonProperty("direct_dial")] 
    public string direct_dial { get; set; } 
    [JsonProperty("job_title")] 
    public string job_title { get; set; } 
    [JsonProperty("company_id")] 
    public int company_id { get; set; } 

} 

然后我做以下,名单都得到填充罚款。怪异的位遇到错误说,在那里我做CompanyDir.first_name该属性不存在,它显然不?:

// This occurs just after the class declaration 
public IList<Notifications> Notes; 
public IList<CompanyDirectory> CompanyDir; 

// Ignore that these both use the same string they're created at different parts of the load process 
CompanyDir = JsonConvert.DeserializeObject<IList<CompanyDirectory>>(responseString); 
Notes = JsonConvert.DeserializeObject<IList<Notifications>>(responseString); 

// Now I thought I should be able to do 
foreach(var s in Notes){ 
    var thename = CompanyDir.first_name.Where(contact_id.Contains(s.sender_id)) 
} 

回答

2

你应该看看了LINQ和Lambda表达式:

var firstNames = CompanyDir.Where(c => c.contact_id == s.sender_id) 
          .Select(c => c.first_name) 
          .ToList(); 

现在你有一个名字列表。一个列表,因为您的Where约束可能有零个或多个匹配。

+0

大声笑,我几乎恨你是多么简单...... – 2014-11-06 09:32:06

+0

嗯,每个ID应该有一个条目在CompanyDir中,如果我将ToList更改为ToString我不会得到first_name我得到System.Linq.Enumerable in Notes.name字段 – 2014-11-06 09:44:44

+0

Aha tis ok了! – 2014-11-06 10:02:27

相关问题