.Where(<condition>).FirstOrDefault() vs .FirstOrDefault(<condition>)

一世执手 提交于 2019-12-19 08:03:27

问题


Using Linq to Entities is there a difference between the following?

db.EntityName.Where(a => a.Id == id).FirstOrDefault();

db.EntityName.FirstOrDefault(a => a.Id == id);

Or is it simply a matter of personal preference?

Thanks.


回答1:


Both generate the same SQL statement. The second approach is shorter, while the first might be clearer to some developers. Ultimately it's a matter of personal preference.

You can inspect the SQL using the ObjectQuery.ToTraceString method.



来源:https://stackoverflow.com/questions/19842794/wherecondition-firstordefault-vs-firstordefaultcondition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!