LInq查询集合内的集合

Bum*_*ble 7 c# linq linq-to-objects

我的对象包含一组集合.我想获取所有子对象ID并将其存储在字符串数组中.

MainObject包含父级列表

父包含儿童列表

子属性是(Id,Name)

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

Dan*_*rth 14

你可以使用SelectMany:

var stringArray = MainObject.ListOfParent
                            .SelectMany(p => p.ListOfChildren
                                              .Select(c => c.Id.ToString()))
                            .ToArray()
Run Code Online (Sandbox Code Playgroud)