时间:2019-03-17 标签:c#syntaxandLinq,IQueryable
这是一个关于 c# 的 SYNTAX 和 NOT 关于我们如何调用/使用 IQueryable 的问题
有人可以向我解释一下吗:
我们有这个声明(System.Linq):
public static double Average<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, int>> selector)
并且调用 Average
double average = fruits.AsQueryable().Average(s => s.Length);
我了解如何调用 Average 以及 IQueryable 的所有类似静态方法 但我不明白声明的语法。
public static double Average<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, int>> selector)
Average
中的
是什么意思 还有这个 IQueryable
。
因为当我们调用它时只有一个参数传递,并且实际的 lambda 表达式 (s => s.Length);
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
部分声明了方法的通用类型参数 - 基本上是序列包含的元素类型。在深入了解 LINQ 之前,您绝对应该了解泛型。然后,
指示该方法的第一个参数:
this
指示它是一个 扩展方法IQueryable
表示参数的类型source
是参数的名称事实它是一种扩展方法可能会让您感到困惑。当您调用时,
编译器会将其转换为
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:
this
indicates that it's an extension methodIQueryable<TSource>
indicates the type of the parametersource
is 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
您正在声明一个扩展方法(即
this
关键字),它将您的方法添加到实现IQueryable
的任何类型,其中TSource
是泛型类型,并且在整个表达式中保持不变。在这种情况下,编译器可以推断出泛型类型,因此在调用方法时无需声明它
You're declaring an extension method (that's the
this
keyword) that adds your method to any type implementingIQueryable<TSource>
whereTSource
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 是您将声明的泛型类型。基本上就是 s 的类型。
TSource is the generic type that you will declare. It's basically what type the s is.
平均
,因为这是 通用方法。该方法可以在任何类型的查询或枚举上运行(只要提供了适合该类型的选择器)。此 IQueryable源
,因为这是一个扩展方法。扩展方法允许您向现有类型添加其他方法,而无需更改该类型的定义。在本例中,Linq 将 Average 方法添加到 IQueryable 接口,而不更改该接口。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.
是该方法的通用参数。此 IQueryablesource
表示扩展方法,这是语法糖。在 C#2.0 中,它只是一个您必须显式调用的静态方法,这样,编译器允许您调用它,就好像它是您调用它的类型的成员一样。<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.this is a keyword for extensions methods: