【发布时间】:2016-11-21 07:46:00
【问题描述】:
我有以下 LINQ 查询,它应该查看表 Products 并向我返回传递参数的记录,这些记录是:搜索词(字符串)和不同“类型”的 3 个布尔值。
var query = context.Products
.Where(a => request.SearchTerm == null || a.Name.Contains(request.SearchTerm))
.Where(a => (request.isTypeA == false || (a.OrderType == "X" && request.isTypeA == true)) ||
(request.typeB == false || (a.OrderType == "R" && request.typeB == true))
|| (request.typeC == false || (a.OrderType == "D" && request.typeC == true)))
.Where (a=> a.OrderType != "U")
.Where(a => a.IsInactiveFlag == false )
.OrderBy(a => a.OrderType)
.Select(c => new ProductType
{
ProductTypeId = c.ProductTypeId,
IsSelected = false,
OrderType = c.OrderType,
Name = c.Name,
IsInactiveFlag = c.IsInactiveFlag
});
问题:问题是查询总是通过查看传递的 searchTerm 返回记录,但它没有考虑布尔参数。所以比如说我的搜索参数是:searchTerm = "reference " , isTypeA = false, isTypeB = false 和 isTypeC = true。上面的查询将返回所有不同类型的 searchTerm "reference" 的所有记录,而不仅仅是 TypeC。
在发布这个问题之前我搜索了很多,但没有什么是我遇到的。请让我知道我做错了什么。 谢谢!
【问题讨论】: