c# syntax and Linq, IQueryable
This is a question about th开发者_开发知识库e 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,
this IQueryable<TSource> source
indicates the first parameter of the method:
this
indicates that it's an extension methodIQueryable<TSource>
indicates the type of the parametersource
is the name of the parameter
The fact that it's an extension method is probably what's confusing you. When you call
query.Average(s => s.Length)
that is converted by the compiler into
Queryable.Average(query, s => s.Length)
You're declaring an extension method (that's the this
keyword) that adds your method to any type implementing IQueryable<TSource>
where TSource
is the generic type, and remains the same throughout the expression.
The compiler can infer the generic type in this case, so you don't need to declare it when calling the method
TSource is the generic type that you will declare. It's basically what type the s is.
Average<TSource>
because this is a Generic Method. The method can be run on a query or enumeration over any type (as long as a suitable selector for that type is provided).
this IQueryable<TSource> source
because this is an Extension Method. Extension methods allow you to add additional methods to existing types without having to alter that type's definition. In this case Linq adds the Average method to the IQueryable interface without altering that interface.
<TSource>
is a generic Parameter of the method.this IQueryable<TSource> source
denotes an extension method, this is syntactic sugar. In C#2.0, it would simply be a static method you'd have to call explicitly, with this, the compiler allows you to call it as if it was a member of the Type you are calling it on.
<X>
is used to make generic functions that work with different types.
X add<X>(X a, X b){return a + b;}
int a = 1;
int b = 2;
int c = add<int>(a,b);
string d = "hello ";
string e = "world";
string f = add<string>(c,d);
this is a keyword for extensions methods:
string putinsidestars(this string x){
return "*" + x + "*";
}
string foo = "bar";
string z = foo.putinsidestars();
// z now contains *bar*
精彩评论