如何在C#中为给定集合创建或筛选Linq-to-SQL



我有三个参数和一个IQueryable<T>

我的参数是:

  • 标题
  • 产品类型
  • 奖励级别

正如您所看到的,ProductTypes本身就是一个数组。

我需要筛选那些标题包含给定标题、具有给定定价级别并且与任何给定产品类型匹配的项目。

换句话说,我需要这个过滤器:

Title and (ProductType1 or ProductTye2 or ProductType3 or ...) and PricingLevel

这是我的代码:

// query is IQueryable<Product>
query = query.Where(i => i.Title.Contains(title))
foreach (var productType in productTypes)
{
query = query.Where(i => i.ProductTypesCsv.Contains(productType)) // here I need OR, instead of And
}
query = query.Where(i => i.PricingLevel == PricingLevel)

如何在中间部分使用OR

删除foreach循环并更改查询

query = query.Where(i => productTypes.Any(pt => i.ProductType == pt));

使用Contains进行此类过滤器:

query = query.Where(i => productTypes.Contains(i.ProductType));