首页 > 其他 > 详细

linq中关于Exists与Find性能比较

时间:2015-12-20 20:35:18      阅读:250      评论:0      收藏:0      [点我收藏+]

1 前几天发现同事写这样的代码

Person enPerson = new Person();
if (lstPerson.Exists(p => p.SName == "张三"))
{
enPerson = lstPerson.Find(p => p.SName == "张三").Age;
}
return enPerson;

我在想,lstPerson.Exists(p => p.SName == "张三") 这一步的目的,不就是为了:  lstPerson.Find(p => p.SName == "张三") 查询出来的实体不为NULL.

于是:我这样写了.

Person enPerson = new Person();
enPerson = lstPerson.Find(p => p.SName == "张三");
if (enPerson != null)
{
   enPerson.Age;
}

经过测试:

Stopwatch sp = new Stopwatch();
sp.Start();
------
sp.Stop();
int k = sp.Elapsed.Milliseconds;

我改过之后的代码 效率比改之前的高一倍.

linq中关于Exists与Find性能比较

原文:http://www.cnblogs.com/nonet/p/5061556.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!