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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Selectis a projection, so what you get is the expressionx=> x.FirstName == "John"evaluated for each element inContextSet()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 likeIEnumerable<bool>(because the type ofx=> x.FirstName == "John"is a bool).Wherefilters the results, returning an enumerable of the original type (no projection).So, use
Selectwhen you want to keep all results, but change their type (project them).Use
Wherewhen you want to filter your results, keeping the original type