比较:LINQ 与 LAMBDA 表达式 [英] Comparison : LINQ vs LAMBDA Expression

查看:32
本文介绍了比较:LINQ 与 LAMBDA 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要讨论一下 LINQ 和 Lambda 表达式的性能.

I need a discussion regarding the Performance of LINQ and Lambda Expression.

哪个更好?

推荐答案

我猜你在这里谈论 LINQ 时指的是查询表达式.

I guess you mean query expression when talking about LINQ here.

它们是等价的.编译器在编译之前将查询表达式改成等价的Lambda表达式,所以生成的IL是完全一样的.

They are equivalent. The compiler changes the query expression into the equivalent Lambda expression before compiling it, so the generated IL is exactly the same.

示例

var result = select s from intarray
             where s < 5
             select s + 1;

完全一样

var result = intarray.Where( s => s < 5).Select( s => s+1);

请注意,如果您像这样编写查询表达式:

Note that if you write the query expression like this:

var result = select s from intarray
             where s < 5
             select s;

转换为:

var result = intarray.Where( s => s < 5);

最后一个 Select 调用被省略,因为它是多余的.

The final call to Select is omitted because it's redundant.

这篇关于比较:LINQ 与 LAMBDA 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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