日期:2014-05-18  浏览次数:20963 次

用泛型或LINQ,如何判断是否有连号
C# code

List<int> a = new List<int>(3);



01、12、23、34、45、56、67、78、89 看作连号

C# code

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是否存在连号呢?

------解决方案--------------------
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))
{
存在
}
------解决方案--------------------
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;
}
}