且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Lambda Expression过滤项目列表

更新时间:2023-09-26 13:17:04

简单:

myList.SelectMany(sublist => sublist)
    .Where(item => item.Name == "ABC" && item.Action == "123");

这将为您提供所有列表中的所有项目.

This gives you all the items inside all the lists.

如果要选择包含该项目的子列表,则:

If you want to select sublists that contain the item instead:

myList.Where(sublist => sublist.Any(item => item.Name == "ABC" && item.Action == "123"));

最后,如果您想保留相同的结构,但只保留与过滤器匹配的项目:

And lastly if you want to preserve the same structure but only keep the items that match the filter:

var newList = myList.Select(sublist => sublist
                       .Where(item => item.Name == "ABC" && item.Action == "123")
                       .ToList()).ToList();