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

请问,这个Linq查询,怎么写
List<int> list1 = new List<int>(){1,2,5,12,10,4,5};

List<int> list2 = new List<int>(){0,1,4,2,7,8,9};


功能要求:
查询list1、list2,要求其元素存在连续关系的个数至少是3个,或以上,求出list1、list2哪个集合满足条件。

上面的只有list2满足条件,其7、8、9是连续的,并且个数是3个,而list1的连续个数1、2或4、5只有2个连续的,所以,不满足条件。


给出查询代码,谢谢!!

------解决方案--------------------
调用
C# code

 List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 };

                List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 };

                bool flag1 = CheckList(list1,3);//false
                bool flag2 = CheckList(list2, 3);//true

------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace wanlinq
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 };

            List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 };

            Console.WriteLine(panduan(list1));

            Console.WriteLine(panduan(list2));

            Console.ReadLine();
        }

        static bool panduan(List<int> myinput)
        {
            List<List<int>> re = new List<List<int>>();
            //默认list里面至少有3个元素
            for (int i = 0; i < myinput.Count - 2; i++)
            {
                List<int> re1 = new List<int>();
                re1.Add(myinput[i]);
                re1.Add(myinput[i + 1]);
                re1.Add(myinput[i + 2]);
                re.Add(re1);
            }

            return re.Any(x =>
            {
                if (x[0] + 1 == x[1] && x[0] + 2 == x[2])
                {
                    return true;
                }
                return false;
            }
                );
        }
    }
}

------解决方案--------------------
C# code
 static    bool getRes (IEnumerable<int> list)
          
     {
            
           if (list==null)
            {
                return false;
                
            }

        return   Enumerable.Range(1, list.Count() - 2).Any(index => {
            int first = list.ElementAt(index - 1);
            int mid = list.ElementAt(index);
            int end = list.ElementAt(index + 1);

            return first < mid && mid < end && (mid * 2 == first + end);
            
            });      

     }

------解决方案--------------------
List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 };

List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 };


Console.WriteLine(Enumerable.Range(0, list2.Count() - 2).Any(index => list2[index] + list2[index + 2] == 2*list2[index + 1]));
Console.Read();
------解决方案--------------------

public static bool ListCheck(System.Collections.Generic.List<int> list)
{
return list.Where((t, index) => index > 0 && index < list.Count() - 1 && t * 2 == list[index - 1] + list[index + 1]).Count() > 0;
}
------解决方案--------------------
探讨

C# code
static bool getRes (IEnumerable<int> list)

{

if (list==null)
{
return false;

}

r……