Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get elements of a List of Lists in C# using linq?

I have List<List < Address>> and I’m trying to retrieve elements in the child list from the parent list, below is what I have done but it is not what i’m trying to achieve

var getChildElement = ParentList
        .Select(x => x.Select(y => y)
                      .Where(z => z.Stud.Res.StudentId == 54));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What about SelectMany()

Projects each element of a sequence to an IEnumerable and
flattens the resulting sequences into one sequence.

var getChildElement = ParentList.SelectMany(x => x.Stud.Res.StudentId == 54);

As described in the documentation, SelectMany() flattens your List<List<Address>> into one sequence and our predicate filters out that sequence into the output.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading