could anyone please explain how OrderByDescending extension method is working?
I surely can.
static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>
(
this IQueryable<TSource> source,
Expression<Func<TSource, TKey>> keySelector);
When you call foo.OrderByDescending( x => bar )
type inference proceeds as follows.
- First a set of bounds for
TSource
is inferred by examining foo
.
- Then we ask "are there any more inferences we can make without knowing TSource?" The answer is no, so we fix the value of
TSource
to the best member of the bound set. Call it S
.
- Then we ask "given the value of
TSource
is S
, can we infer the value of TKey
? Yes. We apply the type value S
to x
, and then infer the type of expression bar
in an environment where x
is of type S
.
- The type of
bar
gives us the type for TKey
and we're done.
If partial type inference is not possible,...
Partial type inference is not impossible. Rather, it's not implemented. It's possible to add the feature to C#; no one has done it.
Regardless, whether or not inference from a partial set of bounds is implemented or possible, there's no difficulty in doing a complete inference of all the types in a call to OrderByDescending
. Obviously we designed the algorithm specifically to be able to handle OrderByDescending
, Join
and so on.