简体   繁体   English

这两个Lambda表达式之间有区别吗?

[英]Is there a difference between these two lambda expressions?

What is the difference if any between these two Lambda Expressions ? 这两个Lambda表达式之间有什么区别? And the second one seems more compact should I always go for that ? 第二个似乎更紧凑,我应该一直这样做吗?

DataContext.Employee.Where(c=>c.id==check_id && c.username==user_name).Select(c=>c.Name).FirstOrDefault();

and

DataContext.Employee.FirstOrDefault(c=>c.id==check_id && c.username==user_name).Name;

The second version can throw a NullReferenceException if a matching element is not found and the default value for the type is null . 如果找不到匹配的元素,并且该类型的默认值为null ,则第二个版本可以引发NullReferenceException

The first version does not have this problem. 第一个版本没有此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以下两个代码片段之间的差异(Lambda表达式) - Difference between below two code snippets (Lambda Expressions) 这两个LINQ表达式有什么区别? - What is the difference between these two LINQ expressions? 成员表达式和Lambda生成的表达式树之间有什么区别? - What is the difference between Member Expressions and an Expression Tree Generated by a Lambda? 构建动态Lambda表达式时,order和select有什么区别? - What is the difference between order and select when build dynamic Lambda Expressions 完整 .NET 框架和 .NET 核心之间 lambda 表达式的差异 - Difference in lambda expressions between full .NET framework and .NET Core 这两个集合初始化表达式的变化有什么区别? - What is the difference between these two variations of collection initialiser expressions? 结合两个Linq lambda表达式 - Combine two Linq lambda expressions 单个查询中有两个lambda表达式 - Two lambda expressions in a single query 如何调试 Expression.Lambda? (.net 核心 2.1 和 .net 核心 3.1 之间表达式树评估的差异) - How to debug Expression.Lambda? (Difference in Expressions tree evaluation between .net core 2.1 and .net core 3.1) dotnet中正则表达式之间的区别 - Difference between the Regex expressions in dotnet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM