Sam Sam - 9 months ago 40
C# Question

LINQ on EF6: Is there difference in terms of performance between query syntax and method calls?

Which syntax results in better performance?

var vRec = (bNoTracking ? tblOrders.AsNoTracking() : tblOrders);
return vRec
.Where(x => (x.WarehouseId == iWarehouseId) && (x.OrderId == iOrderId))


var vRec = (bNoTracking ? tblOrders.AsNoTracking() : tblOrders);
return (from rec in vRec
where (rec.WarehouseId == iWarehouseId) && (rec.OrderId == iOrderId)
select rec)

This question is for EF-6 (for SQL Express 2014) and EF-7 version 7.0.0-rc1-final (for SQLite).

Note: I'm not looking for opinions on coding style difference, only whether there is technical reason to prefer one another.


Both queries will be converted to the same SQL, meaning performance will be identical. It just depends on if you prefer the "fluent" syntax (.Where()) or LINQ query expressions (where).

The SQL generated from my test MSSQL database is as follows, revealed with LINQPad:

enter image description here

This looks to be about as optimized as it'll get, so I'd say no further tweaking is necessary unless you're running this select in some kind of loop.