In Entity Framework, what is the difference between Select and Where?

.net c# entity-framework linq linq-to-sql

Question

What is the difference between .Select() and .Where() in Entity Framework? Eg

return ContextSet().Select(x=> x.FirstName == "John")

vs

ContextSet().Where(x=> x.FirstName == "John")

When should I use .Select vs .Where?

1
51
12/20/2011 4:09:48 PM

Accepted Answer

Select is a projection, so what you get is the expression x=> x.FirstName == "John" evaluated for each element in ContextSet() on the server. i.e. lots of true/false values (the same number as your original list). If you look the select will return something like IEnumerable<bool> (because the type of x=> x.FirstName == "John" is a bool).

Where filters the results, returning an enumerable of the original type (no projection).


So, use Select when you want to keep all results, but change their type (project them).

Use Where when you want to filter your results, keeping the original type

82
11/22/2011 4:42:25 PM

Popular Answer

Where() is a filter.

Select() selects a different piece of data.
Your Select() example will return a collection of booleans.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow