何时在LINQ中使用Lambda表达式代替Where子句 [英] When to use lambda expressions instead of a Where clause in LINQ

查看:71
本文介绍了何时在LINQ中使用Lambda表达式代替Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在深入研究LINQ,并且我试图对这个lambda表达式业务进行哈希处理.我只是没有看到语法的一些细微差别的好处.首先,在我看来,lambda表达式主要只是使用Where子句的另一种方式.那我为什么不只使用Where子句呢? Lambda表达式更有效吗?

I've been really digging into LINQ, and I'm trying to hash out this lambda expression business. I'm just not seeing the benefit of some of the nuances of the syntax. Primarily, it seems to me that a lambda expression is mostly just a different way of using a Where clause. Why wouldn't I just use a Where clause then? Is the lambda expression more efficient?

吸引另一组程序员来使用C#感到更仅仅是语法上的补充吗?我还没有接触过的lambda表达式还有其他更好的用例吗?

Is it just another syntactical addition to draw programmers from another group to feel more comfortable in C#? Are there other better use cases for lambda expressions that I just haven't exposed to yet?

推荐答案

看看本文:

Take a look at this article: LINQ Query Syntax versus Method Syntax :

通常,我们建议使用查询语法 因为它通常更简单,更多 可读性但是没有语义 方法语法和 查询语法.另外,一些 查询,例如检索 匹配一个元素的数量 指定条件,或检索 具有最大值的元素 在源序列中,只能是 表示为方法调用.这 参考文档 中的标准查询运算符 System.Linq命名空间通常使用 方法语法.因此,即使 开始编写LINQ查询, 熟悉如何 在查询和查询中使用方法语法 查询表达式本身.

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. In addition, some queries, such as those that retrieve the number of elements that match a specified condition, or that retrieve the element that has the maximum value in a source sequence, can only be expressed as method calls. The reference documentation for the standard query operators in the System.Linq namespace generally uses method syntax. Therefore, even when getting started writing LINQ queries, it is useful to be familiar with how to use method syntax in queries and in query expressions themselves.

还有这个问题: LINQ:点表示法与查询表达式

这篇关于何时在LINQ中使用Lambda表达式代替Where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆