Skip to main content
 首页 » 编程设计

c#之基于另一个列表过滤列表之LINQ

2024年02月23日16oomusou

我有两个 IEnumerable 列表。

我想根据第一个列表中的结果将值填充到第二个列表中。

第一个 IEnumerable 列表是这样填充的:

IEnumerable<SelectListItem> selectedList = 
CategoryServices.GetAttributesByCategoryID(categoryID); // it returns selected attributes for a particular category 

我有一个获取所有属性的函数。现在我想得到另一个包含所有其他属性的列表(即,selectedList 中不存在的项目)。我试过这个:

IEnumerable<SelectListItem> available = 
CategoryServices.GetAllAttributes().Where(a => !selectedList.Contains(a)); 

但它不是过滤。我正在获取所有属性...有什么想法吗?

请您参考如下方法:

确保您的 SelectListItem类(class)工具 IEquatable<SelectListItem> 所以 Contains()方法具有确定实例相等性的适当方法。