List<int> a = new List<int>(3);
01、12、23、34、45、56、67、78、89 看作连号
List<int> a1 = new List<int>{2,3,8};
List<int> a2 = new List<int>{2,3,2};
List<int> a3 = new List<int>{1,3,9};
List<int> a4 = new List<int>{4,3,0};
List<int> a5 = new List<int>{2,6,9};
如何用LINQ的方法判断 a1、a2、a3、a4、a5是否存在连号呢?

解决方案 »

  1.   

    非要Linq吗? for循环多好判断。
      

  2.   

    List<int> a1 = new List<int>() { 2, 3, 8 };
    if (a1.OrderBy(x => x).Skip(1).Union(new int[] { a1.Min() }).Zip(a1.OrderBy(x => x), (x, y) => x - y).Any(x => x == 1))
    {
        存在
    }
      

  3.   

    void Main()
    {
     List<int> a1 = new List<int>{2,3,8};
    List<int> a2 = new List<int>{2,3,2};
    List<int> a3 = new List<int>{1,3,9};
    List<int> a4 = new List<int>{4,3,0};
    List<int> a5 = new List<int>{2,6,9};
      
    Console.WriteLine(a1.CheckList());
     }static class Ex
    {
       internal  static bool  CheckList(this List<int> list)
    {
      bool flag=false;
      list.Aggregate((x,y)=>
    {
      if(y==x+1)  flag=true; 
    });
    return flag;
    }
    }
      

  4.   

    static class Ex
    {
       internal  static bool  CheckList(this List<int> list)
        {
          bool flag=false;
          list.Aggregate((x,y)=>
            {
              if(y==x+1)  flag=true; 
              return y;           
            });
            return flag;
        }
    }