Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 601
nomad Avatar asked Nov 07 '13 17:11

nomad


1 Answers

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.

like image 196
Ahmad Mageed Avatar answered Oct 15 '22 19:10

Ahmad Mageed