开发者

LINQ Where and FirstOrDefault

开发者 https://www.devze.com 2023-03-25 02:16 出处:网络
string[] s = new string[] {\"cc\", \"aa\", \"bb\", \"\" }; string result = s.FirstOrDefault(开发者_如何转开发x => x.Equals(\"aa\"));
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?

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. The query is rather optimized by LINQ. There are a lot of tools to scan the result query like LINQPad and others.


It depends on the agreement on the coding (code formatting agreement). And no matters for personal use.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号