c# – 简化LINQ表达式

前端之家收集整理的这篇文章主要介绍了c# – 简化LINQ表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一部分我真的不喜欢的代码,如果有可能以某种方式简化它 – 会非常好.
A a; // I want to get rid of this variable
if((a = collection.FirstOrDefault(x => x.Field == null)) != null)
{
  throw new ScriptException("{0}",a.y); //I need to access other field of the object here,that's why I had to declare a variable outside of the expression
}

解决方法

如果要组合变量赋值和定义,则可以使代码更具可读性:
A a = collection.FirstOrDefault(x => x.Field == null);

if(a != null)    
   throw new ScriptException("{0}",a.y);
原文链接:https://www.f2er.com/csharp/97527.html

猜你在找的C#相关文章