LINQ - 查询语法与方法链和方法链拉姆达
在选择在其中一种 LINQ 扩展方法中使用 LINQ 查询语法或 Lambda 表达式时,是否有人遵守任何规则(或者您的雇主强迫您遵守任何规则?)?这适用于任何实体、SQL、对象、任何东西。
在我们的工作场所,我的老板根本不喜欢 lambda,他会使用查询语法来做任何事情,在某些情况下,我发现这种语法的可读性较差。
var names = collection.Select(item => item.Name);
var names = from item in collection
select item.Name;
也许在添加条件时,我发现 Lambda 变得有点混乱,只是
var names = collection.Where(item => item.Name == "Fred")
.Select(item => item.Name);
var names = from item in collection
where item.Name == "Fred"
select item.Name;
出于兴趣:编译器如何处理这个?有谁知道上面的 LINQ 查询如何编译成 lambda 吗?是否会为每个元素调用 Name
属性?我们可以这样做并可能提高性能吗?这是否意味着 lambda 在性能方面稍微更可控?
var names = collection.Select(item => item.Name)
.Where(name => name == "Fred");
当然,当我们开始使用越来越多的表达式时,lambda 会变得混乱,我会开始在这里使用查询语法。
var names = collection.Where(item => item.Name == "Fred")
.OrderBy(item => item.Age)
.Select(item => item.Name);
var names = from item in collection
where item.Name == "Fred"
order by item.Age
select item.Name;
我发现还有一些事情无法使用查询语法完成。您可能认为其中一些非常简单(尤其是聚合函数),但事实并非如此,您必须在末尾添加一种 LINQ 扩展方法,在我看来,使用 lambda 表达式看起来会更整洁。
var names = collection.Count(item => item.Name == "Fred");
var names = (from item in collection
where item.Name == "Fred"
select item).Count()
即使对于一些简单的 lambda 链,ReSharper 也建议我将它们转换为 LINQ 查询。
还有其他人可以补充吗?是否有人有自己的小规则,或者他们的公司是否建议/强制使用一个规则?
Does anyone stick to any rules (or are you forced to stick to any rules by your employer?) when choosing to use either LINQ query syntax or a Lambda expression inside one of the LINQ extension methods? This applies to any Entities, SQL, objects, anything.
At our workplace, my boss doesn't like lambda at all and he'd use the query syntax for anything, which in some cases, I find are less readable.
var names = collection.Select(item => item.Name);
var names = from item in collection
select item.Name;
Maybe when adding a condition, the Lambda I find gets a little messy, where the
var names = collection.Where(item => item.Name == "Fred")
.Select(item => item.Name);
var names = from item in collection
where item.Name == "Fred"
select item.Name;
Just out of interest: how does the compiler treat this one? Does anyone know how the above LINQ query will compile into lambda? Will the Name
property be called for each element? Could we do this instead and potentially improve the performance? Would this mean lambda is slightly more controllable in terms of performance?
var names = collection.Select(item => item.Name)
.Where(name => name == "Fred");
Certainly when we start using more and more expressions, the lambda gets messy and I'd start to use the query syntax here.
var names = collection.Where(item => item.Name == "Fred")
.OrderBy(item => item.Age)
.Select(item => item.Name);
var names = from item in collection
where item.Name == "Fred"
order by item.Age
select item.Name;
There are also a few things that I find can't be done with the query syntax. Some of them you'd think would be really simple (particularly aggregate functions), but no, you have to add one of the LINQ extension methods to the end, which imo, look neater with a lambda expression.
var names = collection.Count(item => item.Name == "Fred");
var names = (from item in collection
where item.Name == "Fred"
select item).Count()
Even for some of the simple lambda chains, ReSharper is suggesting I convert them to LINQ querys.
Can anyone else add to this? Does anyone have their own little rules or does their company suggest/force the use of one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了回答您有关翻译的问题,查询表达式将始终根据 C# 4 规范 7.16 的规则(或 C# 3 规范中的等效规则)进行翻译。在您询问有关
Name
属性的示例中,这不是查询表达式翻译的问题 - 这是Select
和Where 方法使用它们作为参数的委托或表达式树。有时在过滤之前进行投影是有意义的,有时则不然。
至于小规则,我只有一个:使用对相关查询最可读的方式。因此,如果查询发生变化并且“哪种形式更具可读性”同时发生变化,请更改所使用的语法。
如果您要使用 LINQ,您应该对这两种语法感到满意,至少是易于阅读。
我倾向于发现具有多个范围变量的查询(例如通过
SelectMany
或Join
或let
子句)最终使用查询表达式变得更具可读性 - 但这远不是一个硬性规定。To answer your question about translation, the query expression will always be translated based on the rules on 7.16 of the C# 4 spec (or the equivalent in the C# 3 spec). In the example where you're asking the question about the
Name
property, that's not a matter of the query expression translation - it's what theSelect
andWhere
methods do with the delegates or expression trees they take as parameters. Sometimes it makes sense to do a projection before filtering, sometimes not.As for little rules, I only have one: use whichever way is most readable for the query in question. So if the query changes and "which form is more readable" changes at the same time, change the syntax used.
If you're going to use LINQ you should be happy with either syntax, at the very least to read.
I tend to find that queries with multiple range variable (e.g. via
SelectMany
orJoin
, or alet
clause) end up being more readable using query expressions - but that's far from a hard and fast rule.