日期:2014-05-17  浏览次数:20803 次

请问,这个Link方法查询,怎么写?
List<int> list = new List<int>(){6,1,3,0,14,5,7,9,2,1,8,3,2,4};

将以上集合中元素存在连续奇数的,提取出来

提取结果为:{1,3}、{5,7,9}

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 6, 1, 3, 0, 14, 5, 7, 9, 2, 1, 8, 3, 2, 4 };
            List<List<int>> result = new List<List<int>>();
            if (list.Count() < 2) return;
            List<int> cur = list[0] % 2 == 1 ? new List<int>() { list[0] } : new List<int>();
            list.Skip(1).Aggregate(list[0], (pre, current) =>
                {
                    if (current == pre + 2 && pre % 2 == 1)
                        cur.Add(current);
                    if (current != pre + 2 && current % 2 == 1)
                    {
                        if (cur.Count > 1) result.Add(cur.ToList());
                        cur = new List<int>() { current };
                    }
                    return current;
                });
            foreach (List<int> item in result)
                Console.WriteLine("{{ {0} }}", string.Join(", ", item));
        }
    }
}

------解决方案--------------------
		List<int[]> resultsList = new List<int[]>