开发者

Using Where on a list which is contained in another list?

开发者 https://www.devze.com 2023-02-05 10:45 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消