LINQ: difference between 'Select c' and 'Select new (c ...'

What is the difference between these two statements:

 var result = from c in context.CustomerEntities
 join p in context.ProjectEntities on c.Pk equals p.CustomerPk
 where p.Entered > DateTime.Now.AddDays(-15)
 select c; 

and

 var result = from c in context.CustomerEntities
 join p in context.ProjectEntities on c.Pk equals p.CustomerPk
 where p.Entered > DateTime.Now.AddDays(-15)
 select new (c.Company, c.Entered, c.pk);

Is there any performance issue in these statements. (For simplicity, c contains only these 3 coloums.)

Thanks.

+5
source share
4 answers

What is the difference between these two statements

The first returns the filtered sequence of the source / full source object; the second still executes the filter, but returns a sequence of an anonymous type with only these three properties.

Is there a performance issue in these statements

. LINQ-to-Objects, new {...} ( ) , . , LINQ-to-SQL .. ( ), . , , anon; () BLOB ( varchar) , , .

: , DTO:

return new CustomerDto { Company = c.Company, Entered = c.Entered, PK = c.pk};
...
public class CustomerDto { ... }
+10

, , . , .

+2

( ). , Linq-to-SQL ( SQL Server), Linq-to-Entities ( MySQL) Linq-to-Objects ( ). , , , .

: 5000 5 , 400 Linq-to-Entities.

( 1 ): 17314ms

( 5 ): 19193

: 16055

, - ( , ).

+2

, .

But yes, I think the performance overhead. If you follow select c, then the collection will contain links to the source elements. If you do select new { ... }, then C # creates an anonymous type for you, creating new instances of this type and populating them with data. Sounds definitely slower for me.

+1
source

All Articles