如何使用LINQ在2D集合中转换尺寸?

时间:2012-05-11 16:01:55

标签: c# linq

考虑以下结构:

IEnumerable<IEnumerable<int>> collection = new[] { 
    new [] {1, 2, 3}, 
    new [] {4, 5, 6}, 
    new [] {7, 8, 9} 
};

如何枚举此集合,以便获取由第一项,第二项等组成的IEnumerable<int>个集合?

即{1,4,7},{2,5,8},......

(虽然我选择的实现是int[]个对象,但假设您只有IEnumerable<int>个功能。谢谢。)

5 个答案:

答案 0 :(得分:19)

这是一种使用生成器而不是递归的方法。阵列构造也较少,因此可能更快,但这完全是猜想。

public static IEnumerable<IEnumerable<T>> Transpose<T>(
    this IEnumerable<IEnumerable<T>> @this) 
{
    var enumerators = @this.Select(t => t.GetEnumerator())
                           .Where(e => e.MoveNext());

    while (enumerators.Any()) {
        yield return enumerators.Select(e => e.Current);
        enumerators = enumerators.Where(e => e.MoveNext());
    }
}

答案 1 :(得分:3)

Code credit goes here(未经测试,但看起来不错)。

public static class LinqExtensions
{
    public static IEnumerable<IEnumerable<T>> Transpose<T>(this IEnumerable<IEnumerable<T>> values)
    {
        if (!values.Any()) 
            return values;
        if (!values.First().Any()) 
            return Transpose(values.Skip(1));

        var x = values.First().First();
        var xs = values.First().Skip(1);
        var xss = values.Skip(1);
        return
         new[] {new[] {x}
           .Concat(xss.Select(ht => ht.First()))}
           .Concat(new[] { xs }
           .Concat(xss.Select(ht => ht.Skip(1)))
           .Transpose());
    }
}
//Input: transpose [[1,2,3],[4,5,6],[7,8,9]]
//Output: [[1,4,7],[2,5,8],[3,6,9]]
var result = new[] {new[] {1, 2, 3}, new[] {4, 5, 6}, new[] {7, 8, 9}}.Transpose();     

答案 2 :(得分:1)

只需2美分 纯粹的linq:

 var transpond =           collection.First().Select((frow,i)=>collection.Select(row=>row.ElementAt(i)));

或有一些杂质:

var r1 = collection.First().Select((frow, i) => collection.Select(row => row.ToArray()[i]));

答案 3 :(得分:1)

假设所有序列的长度相同。

static void Main(string[] args)
{
    IEnumerable<IEnumerable<int>> collection =
        new[]
        {
            new [] {1, 2, 3},
            new [] {4, 5, 6 },
            new [] {7, 8, 9}
        };
    Console.WriteLine("\tInitial");
    Print(collection);

    var transposed =
        Enumerable.Range(0, collection.First().Count())
                  .Select(i => collection.Select(j => j.ElementAt(i)));
    Console.WriteLine("\tTransposed");
    Print(transposed);
}

static void Print<T>(IEnumerable<IEnumerable<T>> collection)=>
    Console.WriteLine(string.Join(Environment.NewLine, collection.Select(i => string.Join(" ", i))));

礼物:

        Initial
1 2 3
4 5 6
7 8 9
        Transposed
1 4 7
2 5 8
3 6 9

答案 4 :(得分:0)

如果保证所有元素的长度相同,则可以这样做:

IEnumerable<IEnumerable<int>> Transpose(IEnumerable<IEnumerable<int>> collection)
{
    var width = collection.First().Count();
    var flattened = collection.SelectMany(c => c).ToArray();
    var height = flattened.Length / width;
    var result = new int[width][];

    for (int i = 0; i < width; i++)
    {
        result[i] = new int[height];
        for (int j = i, k = 0; j < flattened.Length; j += width, k++)
            result[i][k] = flattened[j];
    }

    return result;
}