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>
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.
Now my problem.
Let's say I have this:
var query = context.Plots.Where(p => p.TrialId == 21);
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' ....
When I do:
var query2 = ((IOrderedQueryable<Plot>)query).ThenBy(p => p.Number);
It compiles, but gives a runtime exception:
Expression of type 'IQueryable`1[Plot]
' cannot be used for parameter of type 'IOrderedQueryable`1[Plot]
' of method 'IOrderedQueryable`1[Plot] ThenBy[Plot,Nullable`1](IOrderedQueryable`1[Plot], Expressions.Expression`1[System.Func`2[Plot,System.Nullable`1[System.Int32]]])
'
The cast is carried out (I checked), but the parameter of ThenBy
is still seen as IQueryable (which puzzles me a bit).
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…