【发布时间】:2011-11-21 05:58:42
【问题描述】:
我想创建一个基于元组“调整”它的 where 子句的单个查询。元组中的第一项包含一个枚举值,指示要过滤的字段。第二个元组项是过滤器值。
请注意下面的查询不起作用:
var query = from p in db.Categories
where ( QueryBy.Item1 == CategoryFields.Name && p.Name == (string)(QueryBy.Item2) ) ||
( QueryBy.Item1 == CategoryFields.Id && p.Id == (long)(QueryBy.Item2) ) ||
( QueryBy.Item1 == CategoryFields.Description && p.Description == (string)(QueryBy.Item2) ) ||
( QueryBy.Item1 == CategoryFields.SortOrder && p.SortOrder == (int)(QueryBy.Item2) )
select...
if (query.Count() == 1) // ERRORS HERE CONVERSION OF INT
仅使用此 where 子句更改的类似查询将起作用:
var query = from p in db.Categories
where ( QueryBy.Item1 == CategoryFields.Name && p.Name == (string)(QueryBy.Item2) )
select...
if (query.Count() == 1) // Works HERE
知道有什么问题吗?是不是 LINQ where 子句执行了短路评估,因此 item2 的演员阵容失败了?有没有更好的方法来实现我调整 where 子句的总体目标?
提前感谢您的帮助!
【问题讨论】:
标签: linq-to-sql