What's faster? select [object] or select new {[object].value1, [object].value2} with linq
I was hoping so开发者_JAVA百科me of you guru's out there know the answer to this question. Which is faster? A or B
A:
var item = from d in database
select d;
B:
var item = from d in database
select new {d.item1, d.item2};
SEEMINGLY, it seems like selecting part of the table object would be faster than selecting the entire object, but something tells me its not necessarily the case.
If the LINQ query is going to be converted to SQL then the second one will be faster since it will return less results from the database.
The first query will return all fields from the database row that corresponds to the entity in question. The second one will only return the two fields that correspond to value1
and value2
.
That all depends how big the object is.
I would normally only do that if performance suffers, and I need to make some lookup tables, etc for optimization.
精彩评论