简体   繁体   中英

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));

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM