开发者

Select the greatest number below a given number using LINQ to Objects?

I have a simple collection of numbers:

Dim listNumbers As List(Of Byte) = New List(Of Byte)({1, 2, 3, 4,开发者_StackOverflow中文版 5})

Using LINQ how can I select the greatest number of this list that is lower than a given number?

For example, if given number = 3, the greatest number is 2!


Here's VB.NET:

Dim listNumbers As List(Of Byte) = New List(Of Byte)({1, 2, 3, 4, 5})
Dim max As Integer = listNumbers.Where(Function(n As Integer) n < 3).Max()
Console.WriteLine(max)

Effectively what we are doing is finding a list of candidates (those numbers that are less than three) and then taking the maximum of the candidates.

If you need C#:

var listNumbers = new List<byte> { 1, 2, 3, 4, 5 };
int max = listNumbers.Where(n => n < 3).Max();
Console.WriteLine(max);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜