C#在两个列表之间创建关系

时间:2014-11-06 09:23:30

标签: c#

我试图从一个iList获取一个名称到另一个iList,它们都包含相同的ID。

    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))
}

1 个答案:

答案 0 :(得分:2)

你应该查找LinQ和Lambda表达式:

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

现在你有一个名字列表。列表,因为Where约束可能有零次或多次点击。