我有一个 linq 查询,我想WHERE
为使用 LinqKit Predicate Builder 添加一些额外的可选条件。但是,我很难让额外的谓词起作用
这是我的初始查询:
var query = (from OP in ctx.OrganisationProducts
where OP.OrganisationID == orgID
orderby OP.Product.Name, OP.Product.VersionName
select OP).Include("Product");
如您所见,里面JOIN
有。然后,如果需要,我希望添加其他谓词:
if(!includeDisabledP42Admin || !includeDisabledOrgAdmin)
{
var pred = PredicateBuilder.True<OrganisationProduct>();
if (!includeDisabledP42Admin)
pred.And(op => op.Enabled);
if (!includeDisabledOrgAdmin)
pred.And(op => op.AccessLevel == "NA" || op.AccessLevel == "NU");
query = query.Where(pred);
}
但是,生成的 SQL 不变,查询返回相同的行数。
我想我可能不得不像这样进行扩展转换:
query = query.AsExpandable().Where(pred);
但这会导致运行时错误。
我认为问题在于我将谓词添加到已经不再是纯OrganisationProduct
对象的查询中,但是我想就如何在正确的位置插入谓词提出建议。
谢谢大家:-)