Using Where on a list which is contained in another list?
Let's say I have a list called list1
. This list1
contains another list, called list2
. Now I want to check if list2
in list1
contains certain elements and return another list.
list3 = list1.list2.Where(p => p.开发者_Go百科something == 1)
Something like that?
This solution will return conditioned items in the inner list.
var result = list.SelectMany(l => l.InnerList)
.Where(p => p.something == 1);
If you want to get items in the outer list which meets the condition, use:
var another = list.Where(l => l.InnerList.Any(p => p.something == 1));
Take a look at the SelectMany function.
精彩评论