相关疑难解决方法(0)

是否可以使用LINQ检查列表中的所有数字是否单调递增?

我很感兴趣,如果在LINQ中有一种方法来检查列表中的所有数字是否单调递增?

List<double> list1 = new List<double>() { 1, 2, 3, 4 };
Debug.Assert(list1.IsIncreasingMonotonically() == true);

List<double> list2 = new List<double>() { 1, 2, 100, -5 };
Debug.Assert(list2.IsIncreasingMonotonically() == false);
Run Code Online (Sandbox Code Playgroud)

我问的原因是我想知道将列表中的元素与其前一个元素进行比较的技术,这是我在使用LINQ时从未理解过的.

C#中的完成示例类

根据@Servy下面的官方回答,这是我现在使用的完整课程.它为您的项目添加扩展方法,以检查列表是单调增加/减少还是严格单调增加/减少.我正在尝试习惯一种函数式编程风格,这是一种很好的学习方法.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyHelper
{
    /// <summary>
    /// Classes to check if a list is increasing or decreasing monotonically. See:
    /// http://stackoverflow.com/questions/14815356/is-it-possible-to-use-linq-to-check-if-all-numbers-in-a-list-are-increasing-mono#14815511
    /// Note the difference between strictly monotonic and monotonic, see:
    /// http://en.wikipedia.org/wiki/Monotonic_function
    /// …
Run Code Online (Sandbox Code Playgroud)

.net c# linq

9
推荐指数
3
解决办法
3538
查看次数

标签 统计

.net ×1

c# ×1

linq ×1