内部LINQ和Lambda表达式之间的区别 [英] Difference between LINQ and Lambda Expression internally

查看:109
本文介绍了内部LINQ和Lambda表达式之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 IList <   ResponseDTO  <  > ();
var myValue = MyList.Select(T => T.KeyId);
var myValue =来自MyList中的myListValue
              选择myListValue.KeyId; 



在上面的代码中,第一个是Lambda Expression&第二个是LINQ,两者都给我相同的结果.

我想知道这两者之间的内部差异以及它们的性能. . ResponseDTO 替换为string ,并且ToUpper 方法用于编译代码.

 IList< string> MyList =  List< string>();
     var  myValue = MyList.Select(T = >  T.ToUpper());

//  IL_0001:newobj System.Collections.Generic.List< System.String> .. ctor 
//  IL_0006:stloc.0 
//  IL_0007:ldloc.0 
//  IL_0008:ldsfld UserQuery.CS 


<> 9__CachedAnonymousMethodDelegate1 // IL_000D:brtrue.s IL_0022 // IL_000F:ldnull // IL_0010:ldftn UserQuery.< Main> b__0 // IL_0016:newobj System.Func< System.String,System.String> .. ctor // IL_001B:stsfld UserQuery.CS


<> 9__CachedAnonymousMethodDelegate1 // IL_0020:brss IL_0022 // IL_0022:ldsfld UserQuery.CS


IList<ResponseDTO> MyList = new List<ResponseDTO>();
var myValue = MyList.Select(T => T.KeyId);
var myValue = from myListValue in MyList
              select myListValue.KeyId;



In the above code the first one is a Lambda Expression & second is LINQ and both give me the same result.

I want to know the internal difference between these two and their performance.

解决方案

Both syntax compile to the same thing to IL as can be seen from the below code. ResponseDTO is replaced with string and ToUpper method is used to compile the code.

IList<string> MyList = new List<string>();
    var myValue = MyList.Select(T => T.ToUpper());

//IL_0001:  newobj      System.Collections.Generic.List<System.String>..ctor
//IL_0006:  stloc.0
//IL_0007:  ldloc.0
//IL_0008:  ldsfld      UserQuery.CS


<>9__CachedAnonymousMethodDelegate1 //IL_000D: brtrue.s IL_0022 //IL_000F: ldnull //IL_0010: ldftn UserQuery.<Main>b__0 //IL_0016: newobj System.Func<System.String,System.String>..ctor //IL_001B: stsfld UserQuery.CS


<>9__CachedAnonymousMethodDelegate1 //IL_0020: br.s IL_0022 //IL_0022: ldsfld UserQuery.CS


这篇关于内部LINQ和Lambda表达式之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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