.NET的LINQ查询语法VS法链 [英] .NET LINQ query syntax vs method chain

查看:202
本文介绍了.NET的LINQ查询语法VS法链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下两个语句之间的性能差异?

Is there any performance difference between the following two statements?

from item in collection 
where item.id == 3
select item

collection.Where(item => item.id ==3)

在一般情况下,是有LINQ的语法和方法链之间的性能差异?

In general, is there any performance difference between the LINQ syntax and the method chain?

推荐答案

没有,因为他们被编译成的完全的同一code。

No, because they are compiled into exactly the same code.

基本上查询EX pressions是pre-处理,无查询EX pressions C#3由编译器成和重载那么规则,拉姆达EX pression翻译等都是正常应用。这是一个非常优雅的系统,这意味着查询EX pressions规则仅限于该规范只是一个小一点。

Basically query expressions are "pre-processed" by the compiler into "C# 3 without query expressions" and then the rules of overloading, lambda expression translation etc are applied as normal. It's a really elegant system which means that the rules for query expressions are limited to just one small bit of the spec.

当然,也有您可以在链接方法语法写各种各样的事情不能写在查询前pression语法,无论是由于使用其他重载或根本不被支持的方法(如计数()) - 但除非你使用这些,编译code将是完全一样的。选择对于任何特定的场景中,最可读的选择。

Of course, there are various things you can write in "chained method" syntax which can't be written in query expression syntax, either due to using other overloads or the methods simply not being supported (e.g. Count()) - but unless you're using those, the compiled code will be exactly the same. Pick the most readable alternative for any particular scenario.

这篇关于.NET的LINQ查询语法VS法链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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