Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is 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 related issue in these statements. (For simplicity c contains only these 3 coloums.)

Thanks.

like image 521
Waheed Avatar asked Aug 10 '09 10:08

Waheed


People also ask

What is difference between Select and SelectMany in LINQ?

Select and SelectMany are projection operators. A select operator is used to select value from a collection and SelectMany operator is used to selecting values from a collection of collection i.e. nested collection.

What is Select new LINQ?

@CYB: select new is used when you want your query to create new instances of a certain class, instead of simply taking source items. It allows you to create instances of a completely different class, or even an anonymous class like in OP's case.

What is the difference between Select * and Select?

Save this answer. Show activity on this post. select * will return values for all the columns in the table that have rows that match your predicate, while select column_name will only return the values in the column_name column for the rows that match your predicate. Save this answer.

What is the difference between where and Select in LINQ?

Select will always return the same number of elements in the list (regardless of a filter condition you may have). Where can return less elements depending on your filter condition.


3 Answers

What is difference between these two statements

The first returns a filtered sequence of the original/complete source object; the second still does the filter, but returns a sequence of an anonymous type with just those three properties.

Is there any performance related issue in these statements

Performance depends on the back-end. If this is LINQ-to-Objects, then with new {...} you are creating extra objects (anonymous types) per record, so there may be a very small overhead. However, if this is LINQ-to-SQL etc (a database back-end), then this can be a huge benefit. The query builder will check which columns are needed, and will only fetch the three in your anon-type; if you have (for example) a BLOB (or just long varchar) in your data that you don't need, this can be a huge benefit.

Additional notes: you can't include anonymous types in the signature of a method, so you might find you need to declare your own DTO type for this purpose:

return new CustomerDto { Company = c.Company, Entered = c.Entered, PK = c.pk};
...
public class CustomerDto { ... }
like image 88
Marc Gravell Avatar answered Nov 03 '22 01:11

Marc Gravell


The main difference is that the first example returns references to existing instances while the second example creates new instances of an anonymous type. I would be more concerned with this issue than any possible performance issues.

like image 27
Brian Rasmussen Avatar answered Nov 03 '22 00:11

Brian Rasmussen


I ran some tests (using Stopwatch). In no cases were anonymous types faster, in Linq-to-SQL (against SQL Server), Linq-to-Entities (against MySQL), and Linq-to-Objects (against a List). In fact, usually it was slower, depending on how many columns you select.

One of my results: I ran each query 5000 times against a 5-column table populated by 400 rows with Linq-to-Entities.

anonymous object (selecting 1 column): 17314ms

anonymous object (selecting 5 columns): 19193ms

source object: 16055ms

Anyway, the best way to find out is to test it yourself (takes about the time to write a good post).

like image 41
Alex Avatar answered Nov 03 '22 00:11

Alex