开发者

How to write it in LINQ?

开发者 https://www.devze.com 2022-12-23 13:29 出处:网络
I am writing the following in LINQ Enumerable开发者_如何学Python.Range(50, 100).Select(n => n/10 == 1)

I am writing the following in LINQ

Enumerable开发者_如何学Python.Range(50, 100).Select(n => n/10 == 1)

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


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.


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);


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消