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.
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.
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() is a filter. Select() selects a different piece of data. Your Select() example will return a collection of booleans.
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.
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