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

循环层数不确定,怎么办?
int[][] list = new int[][];
我要把每个元素的某个元素进行组合,比如有3个就是:
for (i=0;i<list[0].Length;i++)
    for (int j=0;j<list[1].Length;j++)
       for (int k=0;k<list[2].Length;j++)
           int arr = new int[]{list[0][i],list[1][j],list[2][k]};

------解决方案--------------------
额,自行领悟何谓递归

ps:就你的问题本身,请google“c# 笛卡尔积”
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] data = new int[][] 
            { 
                new int[] { 1, 2, 3 },
                new int[] { 4, 5 },
                new int[] { 7, 8 },
                new int[] { 9 }
            };
            var query = data[0].Select(x => new int[] { x });
            foreach (var item in data.Skip(1))
            {
                query = query.SelectMany(x => item.Select(y => x.Concat(new int[] { y }).ToArray()));