This question is an extension of Partial type inference If partial type inference is not possible, could anyone please explain how OrderByDescending extension method is working? Please note that when I call OrderByDescending(m=>m.DateProp), I am not asked to provide Type parameter.
static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>(
   this IQueryable<TSource> source, 
   Expression<Func<TSource, TKey>> keySelector);
                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.
TSource is inferred by examining foo.TSource to the best member of the bound set.  Call it S.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. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With