2011-05-31 103 views
5

我的对象包含集合集合。我喜欢获取所有的子对象ID并将其存储在一个字符串数组中。LInq查询收藏集合

MainObject包含父目录

家长包含儿童名单

儿童属性(ID,姓名)

我如何可以查询MainObject并找到所有子ID,并将其存储在字符串数组使用linq?

回答

11

您可以使用SelectMany

var stringArray = MainObject.ListOfParent 
          .SelectMany(p => p.ListOfChildren 
               .Select(c => c.Id.ToString())) 
          .ToArray() 
+0

由于现在所有的关键是..它的工作是用许多选择 – Bumble 2011-05-31 14:08:03

3
var arrayOfIds = MainObject.ListOfParents 
          .SelectMany(x => x.ListOfChildren) 
          .Select(x => x.Id) 
          .ToArray(); 
+0

感谢所有..它是工作,现在关键正在使用select many。 – Bumble 2011-05-31 14:07:18

4

试试这个

var id =parents.SelectMany(p => p.Children).Select(x => x.Id).ToArray();