问题描述
适用于实体框架,扩展方法选择()
和的OrderBy()
都返回一个的ObjectQuery
,其定义为:
Applied to entity framework, the extension methods Select()
and OrderBy()
both return an ObjectQuery
, which is defined as:
public class ObjectQuery<T> : ObjectQuery, IOrderedQueryable<T>,
IQueryable<T>, <... more interfaces>
的返回类型选择()
是的IQueryable< T>
和的排序依据
是 IOrderedQueryable< T>
。所以,你可以说,这两个返回相同的类型,但在不同的包装。幸运的是这样,因为现在我们可以应用 ThenBy
在排序依据
被调用。
The return type of Select()
is IQueryable<T>
and that of OrderBy
is IOrderedQueryable<T>
. So you could say that both return the same type but in a different wrapper. Luckily so, because now we can apply ThenBy
after OrderBy
was called.
现在我的问题。
让我们说我有这样的:
var query = context.Plots.Where(p => p.TrialId == 21);
这给了我一个的IQueryable<情节>
,这是一个的ObjectQuery<情节>
。但是,这也是一个IOrderedQueryable:
This gives me an IQueryable<Plot>
, which is an ObjectQuery<Plot>
. But it is also an IOrderedQueryable:
var b = query is IOrderedQueryable<Plot>; // True!
但还是:
But still:
var query2 = query.ThenBy(p => p.Number); // Does not compile.
// 'IQueryable<Plot>' does not contain a definition for 'ThenBy'
// and no extension method 'ThenBy' ....
当我做的:
var query2 = ((IOrderedQueryable<Plot>)query).ThenBy(p => p.Number);
它编译,但给出了一个运行时异常:
It compiles, but gives a runtime exception:
类型的表达式 IQueryable`1 [剧情]
'不能用于类型参数'IOrderedQueryable` 1 [剧情]
'方法' IOrderedQueryable`1 [剧情] ThenBy [剧情,Nullable`1](IOrderedQueryable`1 [剧情],Expressions.Expression`1 [系统.Func`2 [剧情,System.Nullable`1 [System.Int32]]])
演员进行(我检查),但参数 ThenBy
仍被视为IQueryable的(这让我为难了一下)。
The cast is carried out (I checked), but the parameter of ThenBy
is still seen as IQueryable (which puzzles me a bit).
现在想一些方法返回一个的ObjectQuery<情节>
我为的IQueryable<情节>
(如选择()
)。如果我想知道它是否是安全的,叫 ThenBy
返回的对象。我怎样才能弄清楚,如果的ObjectQuery
是真或假 IOrderedQueryable
没有赶上产生的异常?
Now suppose some method returns an ObjectQuery<Plot>
to me as IQueryable<Plot>
(like Select()
). What if I want to know whether it is safe to call ThenBy
on the returned object. How can I figure it out if the ObjectQuery
is "real" or a "fake" IOrderedQueryable
without catching exeptions?
推荐答案
表达式树是真的好开心! (也许我是一个怪物一点点),如果的是什么去了! =)
Expression Trees are genuinely good fun! (or perhaps I'm a little bit of a freak) and will likely become useful in many a developer's future if Project Roslyn is anything to go by! =)
在您的情况下,简单的继承MSDN的的,并覆盖 VisitMethodCall
法的东西继承类比较 m.MethodInfo
与 SortBy
(即如果你不强求只需检查名称,如果你想成为挑剔的使用反射来抢实际SortBy的MethodInfo与比较。
In your case, simple inherit from MSDN's ExpressionVisitor, and override the VisitMethodCall
method in an inheriting class with something to compare m.MethodInfo
with SortBy
(i.e. if you're not too fussy simply check the name, if you want to be fussy use reflection to grab the actual SortBy MethodInfo to compare with.
让我知道如果/您所需要的例子,但说实话,复制后/粘贴你可能需要没有ExpressionVisitor超过10行的非表达树码; - )
Let me know if/what you need examples of, but honestly, after copy/pasting the ExpressionVisitor you'll probably need no more than 10 lines of non-expression-tree code ;-)
希望有所帮助。
这篇关于如果是真正的ObjectQuery的IOrderedQueryable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!