开发者

LInq querying collection inside collection

开发者 https://www.devze.com 2023-03-09 18:04 出处:网络
my object contains a collection of collections . i like to get all child object ids and store it in a string array.

my object contains a collection of collections . i like to get all child object ids and store it in a string array.

MainObject contains List of parent

Parent contains List of Child

Child properties are (Id,Name)

how can i query MainObject and find all child i开发者_运维知识库ds and store it in string array using linq?


You can use SelectMany:

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


try this

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


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

精彩评论

暂无评论...
验证码 换一张
取 消