Is it possible to use LINQ to check if all numbers in a list are increasing monotonically?

前端 未结 8 2878
旧巷少年郎
旧巷少年郎 2021-02-20 17:14

I\'m interested if there is a way, in LINQ, to check if all numbers in a list are increasing monotonically?

Example

List l         


        
8条回答
  •  执念已碎
    2021-02-20 18:17

    Would you not order the list using OrderBy() and compare them against the original? If they are the same then it will give your your answer pseudo speaking:

    var increasing = orignalList.OrderBy(m=>m.value1).ToList();
    var decreasing = orignalList.OrderByDescending(m=>m.value1).ToList();
    
    var mono = (originalList == increasing || originalList == decreasing)
    

提交回复
热议问题