LInq collection request inside the collection

my object contains a collection of collections. I like to get all child objects of objects and store them in an array of strings.

MainObject contains a list of parent

Parent contains a list of children

Child Properties: (Id, Name)

How can I query MainObject and find all child identifiers and store it in an array of strings using linq?

+5
source share
3 answers

You can use SelectMany:

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

try it

var id =parents.SelectMany(p => p.Children).Select(x => x.Id).ToArray();
+4
source
var arrayOfIds = MainObject.ListOfParents
                           .SelectMany(x => x.ListOfChildren)
                           .Select(x => x.Id)
                           .ToArray();
+3
source

All Articles