select 来自不包含 linq 中特定项目的列表
select from list not containing particular item in linq
我有两个class。
public class Category
{
public int Id {get;set;}
public string Name{get;set;}
}
public class Product
{
public int ProductId{get;set;}
public string Name{get;set;}
public List<Category> ProductCategory {get;set;}
}
现在我有基于我需要获取产品的类别列表
List<string> filtercategory ={"HomeMade","Uncooked"}.
我需要所有不属于 'HomeMade' 和 'Uncooked' 的产品。
关于这个的 linq 查询逻辑应该是什么?
谢谢。
您可以简单地使用 Where
:
进行过滤
List<Product> productList = GetProducts();
var filteredProducts = productList
.Where(p => p.ProductCategory
.All(pc => !filtercategory.Contains(pc.Name)))
.ToList();
这会过滤 productList
并仅获取 所有 类别名称 不包含 的那些产品 [=13] =].
我只在 linq-to-sql 中测试过这个,没有用实体测试过,所以我不能 100% 确定这也适用于实体 。
我有两个class。
public class Category
{
public int Id {get;set;}
public string Name{get;set;}
}
public class Product
{
public int ProductId{get;set;}
public string Name{get;set;}
public List<Category> ProductCategory {get;set;}
}
现在我有基于我需要获取产品的类别列表
List<string> filtercategory ={"HomeMade","Uncooked"}.
我需要所有不属于 'HomeMade' 和 'Uncooked' 的产品。
关于这个的 linq 查询逻辑应该是什么?
谢谢。
您可以简单地使用 Where
:
List<Product> productList = GetProducts();
var filteredProducts = productList
.Where(p => p.ProductCategory
.All(pc => !filtercategory.Contains(pc.Name)))
.ToList();
这会过滤 productList
并仅获取 所有 类别名称 不包含 的那些产品 [=13] =].
我只在 linq-to-sql 中测试过这个,没有用实体测试过,所以我不能 100% 确定这也适用于实体 。