views:

109

answers:

2

I was hoping some 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.

+4  A: 

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.

Andrew Hare
Ya thats what I thought. I wasn't sure if the entire table row would be returned, and THEN the two items were retrieved, or if it was the way you said where only the two items from the row are retrieved.
Darcy
Besides this, because of the anonymous type, their will be no change tracking, what also saves a bit.
Steven
A: 

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.

leppie