LINQ查询字典<string,object =“”>和List <object> </object> </string,>

时间:2014-05-27 09:49:37

标签: c# .net linq list dictionary

我有

1)列表&lt; objectA&gt; objectAList

objectA有id,姓名,年龄,身高,体重

2)字典&lt; string,List&lt; ObjectB&gt;&gt; objectBDictionary;

对象B有id,姓名,地址,电子邮件

现在,我想提取objectAList和objectBDictionary之间的相关条目匹配(id和name)。

1 个答案:

答案 0 :(得分:1)

我会尝试这样的事情:

// Initially, I would get a list of all objectB's in the lists of objectB that
// are contained in the Dictionary values.

List<ObjectB> objectsB =  objectBDictionary.Select(x=>x.Value)
                                           .SelectMany(y=>y);

// Then, I would make the join.
var result = from a in objectListA
             join b in objectsB
             on new { a.id, a.name } equals { b.id, b.name }
             select new 
             {
                 Id = a.id,
                 Name = a.name,
                 Height = a.height,
                 Weight = a.weight,
                 Address = b.address,
                 Email = b.email
             };