[英]LINQ Where and FirstOrDefault
string[] s = new string[] {"cc", "aa", "bb", "" };
string result = s.FirstOrDefault(x => x.Equals("aa"));
Would one prefer the code above without the .Where() statement or with it? 如果没有.Where()语句或没有它,是否愿意使用上面的代码?
string[] s = new string[] {"cc", "aa", "bb", "" };
string result = s.Where(x => x.Equals("aa")).FirstOrDefault();
Both forms are equivalent, use the one you prefer. 两种形式都是等效的,请使用您喜欢的形式。 I don't think it makes a big difference in terms of performance... I tend to chose the first form, because it's shorter.
我认为这对性能没有太大影响。我倾向于选择第一种形式,因为它较短。
You should really study IQueryable object, it does not work in order you specify. 您应该真正研究IQueryable对象,它不能按您指定的顺序工作。 The query is rather optimized by LINQ.
LINQ对该查询进行了优化。 There are a lot of tools to scan the result query like LINQPad and others.
有很多工具可以扫描结果查询,例如LINQPad等。
It depends on the agreement on the coding (code formatting agreement). 它取决于编码协议(代码格式化协议)。 And no matters for personal use.
而且没有任何私人用途。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.