This is a question about the SYNTAX of c# and NOT about how we call/use IQueryable
Can someone please explain to me:
We have this declaration (System.Linq):
public static double Average<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, int>> selector)
and to call the Average
double average = fruits.AsQueryable().Average(s => s.Length);
I understand how to call the Average and all the similar static method of IQueryable
but I don’t understand the syntax of the declaration.
public static double Average<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, int>> selector)
What does the <TSource> mean in Average<TSource>(
and also the this IQueryable<TSource> source.
since only one parameter passes when we call it and the actual lambda expression (s => s.Length);
Thanks in advance.
The
<TSource>part declares the generic type parameters of the method – basically what kind of element the sequence contains. You should definitely understand generics before you get too far into LINQ.Then,
indicates the first parameter of the method:
thisindicates that it’s an extension methodIQueryable<TSource>indicates the type of the parametersourceis the name of the parameterThe fact that it’s an extension method is probably what’s confusing you. When you call
that is converted by the compiler into