时间:2019-03-17 标签:c#syntaxandLinq,IQueryable

发布于 2024-09-02 10:08:26 字数 982 浏览 2 评论 0原文

这是一个关于 c# 的 SYNTAXNOT 关于我们如何调用/使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

巷雨优美回忆 2024-09-09 10:08:26

部分声明了方法的通用类型参数 - 基本上是序列包含的元素类型。在深入了解 LINQ 之前,您绝对应该了解泛型。

然后,

this IQueryable<TSource> source

指示该方法的第一个参数:

  • this 指示它是一个 扩展方法
  • IQueryable表示参数的类型
  • source是参数的名称

事实它是一种扩展方法可能会让您感到困惑。当您调用时,

query.Average(s => s.Length)

编译器会将其转换为

Queryable.Average(query, s => s.Length)

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 method
  • IQueryable<TSource> indicates the type of the parameter
  • source 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)
寄人书 2024-09-09 10:08:26

您正在声明一个扩展方法(即 this 关键字),它将您的方法添加到实现 IQueryable 的任何类型,其中 TSource 是泛型类型,并且在整个表达式中保持不变。

在这种情况下,编译器可以推断出泛型类型,因此在调用方法时无需声明它

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

别在捏我脸啦 2024-09-09 10:08:26

TSource 是您将声明的泛型类型。基本上就是 s 的类型。

TSource is the generic type that you will declare. It's basically what type the s is.

梦巷 2024-09-09 10:08:26

平均,因为这是 通用方法。该方法可以在任何类型的查询或枚举上运行(只要提供了适合该类型的选择器)。

此 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.

音盲 2024-09-09 10:08:26
  1. 是该方法的通用参数。
  2. 此 IQueryablesource 表示扩展方法,这是语法糖。在 C#2.0 中,它只是一个您必须显式调用的静态方法,这样,编译器允许您调用它,就好像它是您调用它的类型的成员一样。
  1. <TSource> is a generic Parameter of the method.
  2. 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.
你穿错了嫁妆 2024-09-09 10:08:26

用于创建适用于不同类型的通用函数。

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);

这是扩展方法的关键字:

string putinsidestars(this string x){
  return "*" + x + "*";
}

string foo = "bar";
string z = foo.putinsidestars();
// z now contains *bar*

<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*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文