Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LInq querying collection inside collection

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 ids and store it in string array using linq?

like image 294
Bumble Avatar asked May 31 '11 13:05

Bumble


2 Answers

You can use SelectMany:

var stringArray = MainObject.ListOfParent
                            .SelectMany(p => p.ListOfChildren
                                              .Select(c => c.Id.ToString()))
                            .ToArray()
like image 156
Daniel Hilgarth Avatar answered Sep 28 '22 07:09

Daniel Hilgarth


try this

var id =parents.SelectMany(p => p.Children).Select(x => x.Id).ToArray();
like image 35
Pranay Rana Avatar answered Sep 28 '22 05:09

Pranay Rana