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.
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.
@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.
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.
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.
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 { ... }
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With