Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Select and Where in Entity Framework

People also ask

What is the difference between select and SelectMany?

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 does select () do in C#?

In a query expression, the select clause specifies the type of values that will be produced when the query is executed. The result is based on the evaluation of all the previous clauses and on any expressions in the select clause itself.

What is select method in Linq?

The Select() method invokes the provided selector delegate on each element of the source IEnumerable<T> sequence, and returns a new result IEnumerable<U> sequence containing the output of each invocation.


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


Where() is a filter.

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