LINQ Object Query Performance
I have a stupid question. I would like to know if there is a performance difference between these two queries:
var cObject = from cust in entities.Customer
where cust.id == cid
select cust;
and
var cObject = entities.Customer.First( c=> c.id == cid);
My query only returns one record as I am querying for the primary key. But don't they matter?
a source to share
As Craig Stunts points out in the comments on this question, you should be careful with profiling your Linq code, as delayed lazy loading can skew the profiling results.
To get a clear read, you need to call ToList()
to force the request, and then you have to do the same in the second example to make it apples apples.
In any case, if the second query were rewritten to return an IEnumerable IQueryable with one client object instead of one client object, the two statements would be functionally identical.
a source to share
No performance.
For the first time Microsoft introduced
var cObject = entities.Customer.First( c=> c.id == cid);
Edit: returns IQueryable
Then they added sugar to it to make the job easier.
var cObject = from cust in entities.Customer
where cust.id == cid
select cust;
Edit Returns customer object
a source to share