【发布时间】:2018-04-03 05:44:45
【问题描述】:
我正在尝试使用来自LINQ to SQL Where Clause Optional Criteria的答案
我喜欢我的 linq 使用基于查询的语法。不知道如何使用 whereif。
我的查询看起来像这样
var result = (from tran in _ctx.Transactions where tran.Id == transactionId select tran);
有时会投射出来
var result = (from tran in _ctx.Transactions where tran.Id == transactionId
select new Abstract
{
tran.Date,
tran.Key
});
我可以使用方法语法做一个可选的过滤器
var result = _ctx.Transactions
.where(t=>t.Id == transactionId)
.whereIf(tran.Dept!= "AllDept", x => x.Dept== deptName);
不确定如何在基于查询的 linq 查询中使用 WhereIf。
【问题讨论】:
-
参见stackoverflow.com/questions/279701/… 讨论哪种语法更好,特别是在您扩展它时。
-
那个语法很残缺,所以你能做的最好的就是:
from tran in _ctx.Transactions.WhereIf(..., tran => tran.Id == transactionId) select tran(所以是两种语法的混合)。