且构网

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

按另一个列表过滤列表 C#

更新时间:2023-11-23 11:57:22

如果您遇到以下情况:

List<ItemBO> items;
List<ItemCategoryBO> categories;

并且您希望获得属于您的类别列表中的类别的所有项目,您可以使用:

and you wish to get all the items that have a category that is in your list of categories, you can use this:

IEnumerable<ItemBO> result = items.Where(item =>
    categories.Any(category => category.ItemCategory.equals(item.ItemCategory))); 

Any 运算符枚举源序列,如果任何元素满足谓词给出的测试,则返回 true.在这种情况下,如果类别列表包含 ItemCategoryBO,其中它的 ItemCategory 字符串与项目的 ItemCategory 字符串相同,则它返回 true.关于它的更多信息 MSDN

The Any operator enumerates the source sequence and returns true if any element satisfies the test given by the predicate. In this case, it returns true if the categories list contains an ItemCategoryBO where its ItemCategory string is the same as the item's ItemCategory string. More information about it on MSDN