Sam
Sam

Reputation: 1898

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))
     .FirstOrDefault<tblOrder>();

OR

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

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.

Upvotes: 0

Views: 750

Answers (1)

Scott
Scott

Reputation: 5369

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.

Upvotes: 3

Related Questions