Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between select and where in LINQ [duplicate]

Tags:

c#

linq

Possible Duplicate:
Linq: What is the difference between Select and Where

What's the difference between

var a = Doc.Document.Where(n => n.Id == id).SingleOrDefault(); 

and

var b = Doc.Document.Select(n => n.Id == id).SingleOrDefault();

Why variable b is a boolean ?

Sorry about my ignorance, I am new to LINQ.

like image 798
kevin Avatar asked Feb 27 '12 09:02

kevin


People also ask

What is difference between select and where 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.

How do I find duplicate records in LINQ?

The easiest way to solve the problem is to group the elements based on their value, and then pick a representative of the group if there are more than one element in the group. In LINQ, this translates to: var query = lst. GroupBy(x => x) .

Where vs select SQL?

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


1 Answers

Where Filters a sequence of values based on a predicate. So in the first example you are selecting elements from your list where the function n.Id == id is true.

Select Projects each element of a sequence into a new form, so in your second example you get a list of booleans which is the result of the function n.Id == id on each element.

like image 180
Sam Holder Avatar answered Oct 05 '22 15:10

Sam Holder