[英]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.