tags:

views:

102

answers:

3

I am writing the following in LINQ

Enumerable.Range(50, 100).Select(n => n/10 == 1)

but it's not working. How to write the above query?

+6  A: 

Since your expression is a predicate -- and from your comment you want to return it into an IEnumerable<int> -- I'm guessing you actually want to filter the source collection rather than project it into a sequence of booleans. If that's correct, you need the Where operator rather than Select:

var intsBetween10And19 = ints.Where(n => (n/10 == 1));

Select performs a projection, i.e. it "returns" the value of the select expression (in this case a boolean). Where is the filtering operator.

itowlson
+1. The `Select` operator would just then select `IEnumerable<bool>` containing whether or not each element fit the condition.
Adam Robinson
+1  A: 

If you want all the number from 50 to 100 that are divisible by 10 then you need this...

var res =  Enumerable.Range(50, 51).Where(n=>n%10==0);
Matthew Whited
thank you matthew whited.
Veejay
Glad I could help. Next time you may try to provide an entire line of code and what you expect. It may save on the confusion.
Matthew Whited
yes I will remember that and paste entire code. Sorry everyone for the confusion.
Veejay
A: 

Since you are trying to get all of the values divisible by 10 (as per your comment) you need this:

Enumerable.Range(50, 100).Select(n => n % 10 == 0)
Enigmativity
this is correct if you just want a set of bools... if you want the int values look at my result (by the way his range is 50 to 100 so it should be 50,51)
Matthew Whited