简体   繁体   English

是Linq还是Lambda?

[英]Is it Linq or Lambda?

I know that this is Linq: 我知道这是Linq:

var _Results = from item in _List
                where item.Value == 1
                select item;

And I know this is Lambda: 我知道这是Lambda:

var _Results = _List.Where(x => x.Value == 1);

Editor's note: the above is not merely Lambda, it is Linq using the "Method Syntax" whose predicate is a Lambda. 编者注:以上不仅仅是Lambda,Linq使用的是“方法语法”,其谓词是Lambda。 To be clear, both of the above samples are Linq (my original post was incorrect, but I left the error to illustrate the confusion prompting the question). 要明确的是,以上两个样本都是Linq(我的原始帖子不正确,但我留下了错误来说明提示问题的混乱)。

But is Linq a subset of Lambda or what? 但是Linq是Lambda的子集还是什么?

Why are there two seemingly identical techs? 为什么有两个看似相同的技术?

Is there a technical reason to choose one over the other? 是否有技术上的理由来选择其中一个?

This is LINQ (using query syntax): 这是LINQ(使用查询语法):

var _Results = from item in _List
                where item.Value == 1
                select item;

This is also LINQ (using method syntax): 这也是LINQ(使用方法语法):

var _Results = _List.Where(x => x.Value == 1);

It's interesting to note that both of these flavors will end up producing the exact same code. 有趣的是, 这两种口味最终会产生完全相同的代码。 The compiler offers you a service by allowing you to express your wishes in the manner that you prefer. 编译器允许您以您喜欢的方式表达您的意愿,为您提供服务。

And this is a lambda: 是一个lambda:

x => x.Value == 1

When you choose to use method syntax, LINQ is almost always seen around lambda expressions. 当您选择使用方法语法时,LINQ几乎总是在lambda表达式中出现。 But LINQ and lambdas are two totally different things, both of which can be used by themselves. LINQlambdas是两个完全不同的东西,两者都可以自己使用。

Update: As svick rightly points out, LINQ with query syntax is also implemented using lambda expressions (as mentioned earlier, the compiler allows you to write in query syntax but effectively transforms it to method syntax behind your back). 更新:正如svick正确指出的那样,带有查询语法的LINQ 使用lambda表达式实现(如前所述,编译器允许您使用查询语法编写,但有效地将其转换为背后的方法语法)。 This is just piling on the fact that both flavors are totally equivalent and will behave the same way (eg lambda expressions may cause closures to be created). 这仅仅是因为这两种风格完全等效并且行为方式相同(例如,lambda表达式可能会导致创建闭包 )。

Both are Linq. 两者都是Linq。 The second one is using Lambdas . 第二个是使用 Lambdas

Lambdas are the inline method type things that you are passing as a parameter to the Where function in the second example. Lambdas是内联方法类型的东西,您将作为参数传递给第二个示例中的Where函数。

The difference between those two syntaxes is purely syntactic. 这两种语法之间的区别纯粹是语法。 The second linq style using method calls is how it works under the hood. 使用方法调用的第二个linq样式是它在引擎盖下的工作方式。 The first is meant to be more user friendly/easier and the compiler converts it to method calls behind the scenes. 第一个是更加用户友好/更容易,编译器将其转换为幕后方法调用。 They should work the same for any given query though of course the compiler may choose a sligthly different interpretation of a complicated linq query than you would when converting to method style. 对于任何给定的查询,它们应该工作相同,当然编译器可能会选择对转换为方法样式时复杂的linq查询的不同解释。

This msdn article may be of interest too: LINQ Query Syntax versus Method Syntax . 这篇msdn文章也可能是有趣的: LINQ查询语法与方法语法 Of particular relevance is: "In general, we recommend query syntax because it is usually simpler and more readable; however there is no semantic difference between method syntax and query syntax." 特别相关的是:“一般来说,我们建议使用查询语法,因为它通常更简单,更易读;但是方法语法和查询语法之间没有语义差异。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM