了解 =>在LINQ中

发布于 2024-09-30 23:49:01 字数 697 浏览 2 评论 0原文

我正在使用 LINQpad 来掌握 LINQ,在其中一个 XML 示例中,他们有以下代码,

var bench =
    new XElement ("bench",
        new XElement ("toolbox",
            new XElement ("handtool", "Hammer"),
            new XElement ("handtool", "Rasp")
        ),
        new XElement ("toolbox",
            new XElement ("handtool", "Saw"), 
            new XElement ("powertool", "Nailgun")
        ),
        new XComment ("Be careful with the nailgun")
    );

var toolboxWithNailgun =
    from toolbox in bench.Elements()
    where toolbox.Elements().Any (tool => tool.Value == "Nailgun")
    select toolbox.Value;

我对 tool => 很好奇。 tool.Value == "Nailgun"

到底发生了什么?

I am using LINQpad to get a grasp of LINQ and in one of the XML examples they have the following code

var bench =
    new XElement ("bench",
        new XElement ("toolbox",
            new XElement ("handtool", "Hammer"),
            new XElement ("handtool", "Rasp")
        ),
        new XElement ("toolbox",
            new XElement ("handtool", "Saw"), 
            new XElement ("powertool", "Nailgun")
        ),
        new XComment ("Be careful with the nailgun")
    );

var toolboxWithNailgun =
    from toolbox in bench.Elements()
    where toolbox.Elements().Any (tool => tool.Value == "Nailgun")
    select toolbox.Value;

I am curious about tool => tool.Value == "Nailgun"

What exactly is going on there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

萌︼了一个春 2024-10-07 23:49:01

这是一个 C# Lambda 表达式,用于快速指定匿名函数。如果 tool.Value 为“Nailgun”,则此特定 lambda 函数返回 true

That's a C# Lambda Expression which are used to quickly specify anonymous functions. This particular lambda function returns true if the tool.Value is "Nailgun"

一个人的旅程 2024-10-07 23:49:01

Lambda 表达式,IOW 是内联定义的函数。

编译器实际上将 Lambda 转换为一个类,该类具有引用 Lambda 所做的所有操作的字段。如果打开包含 lambda 表达式的程序集,您将看到这些具有奇怪名称的类,例如 b_0() : Void

Lambda 还依赖于编译器的大量类型推断。虽然您可以明确 lambda 中涉及的类型,但通常可以让编译器自行计算出来。毫无疑问,编译器会确保 lambda 是强类型的,即使您没有指定它们。

该函数:

public string Foo(int bar)
{
  return bar.ToString();
}

可以通过以下方式表示为 lambda:

x => x.ToString()

bar => bar.ToString()

int bar => bar.ToString()

(int bar) =>
{
  return bar.ToString();
}

它们都是等价的。

Lambda expression, IOW a function defined inline.

The compiler actually turns the Lambda into a class which has fields that reference everything the lambda does. If you open an assembly that contains lambda expressions, you'll see these classes with weird names such as <string>b_0() : Void.

Lambdas also rely on lots of type inference by the compiler. While you can be explicit about the types involved within the lambda, you can usually let the compiler figure it out on its own. Have no doubt that the compiler ensures the lambda is strongly typed, even though you don't specify them.

This function:

public string Foo(int bar)
{
  return bar.ToString();
}

can be expressed as lambdas in the following ways:

x => x.ToString()

bar => bar.ToString()

int bar => bar.ToString()

(int bar) =>
{
  return bar.ToString();
}

They are all equivalent.

蓝颜夕 2024-10-07 23:49:01

它称为 lambda,本身并不是 LINQ 功能。

这意味着它将匹配工具值等于“Nailgun”的任何元素。

It's called a lambda and isn't a LINQ feature per-se.

It means it will match any elements where the tool value is equal to "Nailgun".

唯憾梦倾城 2024-10-07 23:49:01

这将是 Lambda 运算符 的经典示例(在Lambda 表达式)。

That would be a classic example of the Lambda Operator (in action inside of a Lambda Expression).

赤濁 2024-10-07 23:49:01

=>lambda 运算符 C#。在本例中,您将调用 Linq Any() 函数,该函数采用谓词,该函数采用一些参数并返回布尔值。谓词通常用于确定是否选择某个项目。 Linq 为集合中的每个工具调用此 lambda 谓词。

=> 的左侧是 lambda 参数,在本例中是要测试的工具,右侧是一个布尔表达式,仅当工具的值为“Nailgun”时才返回 true。因此,仅当工具之一是射钉枪时,Any() 函数才返回 true。

=> is the lambda operator in C#. In this case, you're calling the Linq Any() function which takes a predicate, a function which takes some parameter(s) and returns a boolean. A predicate is often used to determine whether to select an item or not. Linq calls this lambda predicate for every tool in the collection.

On the left of the => is the lambda argument, in this case the tool to be tested and on the right is a boolean expression which returns true only when the tool's Value is "Nailgun". Therefore, the Any() function returns true only if one of the tools is a nail gun.

潇烟暮雨 2024-10-07 23:49:01

这意味着任何其 Value 属性(即每个元素中的字符数据)等于“Nailgun”的工具。当 Any() 遍历工具箱中的工具时,将执行 lambda 表达式,并从 lambda 表达式返回其结果(布尔值)。

That means any tool whose Value property (which is the character data in each element) is equal to "Nailgun". As Any() walks the tools in the toolbox, the lambda expression is executed and its result (a bool) is returned back from the lambda expression.

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