简体   繁体   English

LINQ - 动态“查询语法”的子句

[英]LINQ - Dynamic Where Clause on “Query Syntax”

I have been searching about how to make dynamic queries and everything that I found was using "Method Syntax". 我一直在搜索如何进行动态查询以及我发现的所有内容都使用“方法语法”。

Is it possible create dynamic predicates for "Query syntax" ? 是否可以为“查询语法”创建动态谓词?

I tried to use something like 我试着用类似的东西

Expression<Func<TEntity, bool>>

inside the predicate but the compiler return the following message 在谓词内,但编译器返回以下消息

"Cannot convert Expression<Func<TEntity, bool>> to bool"

it works on ' Method Syntax ' but not on ' Query Syntax ' 它适用于' 方法语法 '但不适用于' 查询语法 '

It works: 有用:

Expression<Func<Localization, bool>> locClause = (l => l.id == locId);

var results = UnitOfWork.Localization.AsQueryable().Where(locClause).ToList();

It doesn't work: 它不起作用:

Expression<Func<Localization, bool>> locClause = (l => l.id == locId);

var result = from l in UnitOfWork.Localization.AsQueryable()
                          where locClause 
                          select l;

Is there a way to do this ? 有没有办法做到这一点 ?

Not knowing what you need Expression for, I can't be sure this will do everything you need it to. 不知道你需要什么Expression ,我不能确定这将做你需要的一切。 The use of AsQueryable has me suspect you don't (if you're directly querying a provider, you should already be IQueryable<Localization> ); 使用AsQueryable让我怀疑你没有(如果你直接查询提供者,你应该已经是IQueryable<Localization> ); but, you'll have to confirm. 但是,你必须确认。 But, if you don't need to use Expression , you could do something like this: 但是,如果您不需要使用Expression ,您可以执行以下操作:

Func<Localization, bool> locClause = (l => l.id == locId);

var result = from l in UnitOfWork.Localization.AsQueryable()
                          where locClause(l)
                          select l;

Or with a Predicate<T> : 或者使用Predicate<T>

Predicate<Localization> locClause = l => l.id == locId;

var result = from l in UnitOfWork.Localization.AsQueryable()
                          where locClause(l)
                          select l;

This, of course, means the delegate is executed on the client and not translated into something run on the server (ie part of the T-SQL generated by the provider, if that is indeed happening now). 当然,这意味着委托在客户端上执行,而不是转换为服务器上运行的东西(即提供者生成的T-SQL的一部分,如果现在确实发生的话)。 If you need that, you'd need to continue using the Expression and continue to use the method chain syntax: 如果需要 ,您需要继续使用Expression并继续使用方法链语法:

var result = UnitOfWork.Localization.AsQueryable().Where(locClause);

I don't believe there's any reason to choose Predicate<T> over Func<T,bool> , other than Predicate<T> is more explicit wrt to intention. 我不相信有任何理由选择Predicate<T>不是Func<T,bool> ,除了Predicate<T>更明确的意图。

There's also no functional benefit to using query syntax over method chain syntax--just readability/maintainability. 使用查询语法而不是方法链语法也没有功能上的好处 - 只是可读性/可维护性。 I frequently find that to do much in the way of anything complex with providers usually results in dropping down to method chain syntax. 我经常发现,对于任何与提供者复杂的方式做很多事情通常会导致下降到方法链语法。 I usually just use query syntax with LINQ To Objects. 我通常只使用LINQ To Objects的查询语法。

No, you're going to need to use method syntax. 不,您将需要使用方法语法。 There is no way to provide an expression that you have in a variable as the parameter to a method using query syntax. 无法在变量中提供表达式作为使用查询语法的方法的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM