using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Test
    {
        public string Name { get; set; }
    }
    class Program
    {

        static void Main(string[] args)
        {
            var listofInts = new List<List<Test>>();
            List<Test> a = new List<Test>();
            a.Add(new Test() { Name = "111" });
            a.Add(new Test() { Name = "222" });
            a.Add(new Test() { Name = "333" });
            listofInts.Add(a);

            List<Test> b = new List<Test>();
            b.Add(new Test() { Name = "444" });
            b.Add(new Test() { Name = "555" });
            b.Add(new Test() { Name = "666" });
            listofInts.Add(b);

            List<Test> c = new List<Test>();
            c.Add(new Test() { Name = "777" });
            c.Add(new Test() { Name = "888" });
            c.Add(new Test() { Name = "999" });
            listofInts.Add(c);


            List<Test> d = new List<Test>();
            d.Add(new Test() { Name = "aaa" });
            d.Add(new Test() { Name = "ddd" });
            d.Add(new Test() { Name = "ccc" });
            listofInts.Add(d);
           
            var temp = CrossJoinLists(listofInts);
            foreach (var l in temp)
            {
                foreach (var i in l)
                    Console.Write(i.Name + ",");
                Console.WriteLine();
            }           
        }

        private static IEnumerable<List<T>> CrossJoinLists<T>(IEnumerable<List<T>> listofObjects)
        {
            var result = from obj in listofObjects.First()
                         select new List<T> { obj };

            for (var i = 1; i < listofObjects.Count(); i++)
            {
                var iLocal = i;
                result = from obj in result
                         from obj2 in listofObjects.ElementAt(iLocal)
                         select new List<T>(obj) { obj2 };
            }

            return result;
        }
    }
}

posted on 2012-12-27 11:45  hegang  阅读(420)  评论(0编辑  收藏  举报