了解 =>在LINQ中
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个 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"
Lambda 表达式,IOW 是内联定义的函数。
编译器实际上将 Lambda 转换为一个类,该类具有引用 Lambda 所做的所有操作的字段。如果打开包含 lambda 表达式的程序集,您将看到这些具有奇怪名称的类,例如
b_0() : Void
。Lambda 还依赖于编译器的大量类型推断。虽然您可以明确 lambda 中涉及的类型,但通常可以让编译器自行计算出来。毫无疑问,编译器会确保 lambda 是强类型的,即使您没有指定它们。
该函数:
可以通过以下方式表示为 lambda:
它们都是等价的。
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:
can be expressed as lambdas in the following ways:
They are all equivalent.
它称为 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".
这将是 Lambda 运算符 的经典示例(在Lambda 表达式)。
That would be a classic example of the Lambda Operator (in action inside of a Lambda Expression).
=>
是 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.这意味着任何其 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.