11

私はこのような一次元のコレクションを持っています:

[1,2,4,5.....n]

そのコレクションを次のような2次元コレクションに変換したいと思います。

[[1,2,3],
[4,5,6],
...]

基本的に、必要に応じて、配列を「n」個のメンバーのグループにグループ化または分割します。

ステートメントでそれを行うことはできますが、foreach現在LINQを学習しているので、すべての要素を反復処理して新しい配列を手動で作成する代わりに、LINQ機能を使用したいと思います(該当する場合)

これを実現するのに役立つLINQ関数はありますか?

私は考えていました、GroupByまたはSelectMany私は彼らが私を助けるかどうかわかりませんが、彼らはかもしれません

どんな助けでも本当に感謝します=):**

4

7 に答える 7

18

次のように、インデックスをバッチサイズで割ってグループ化できます。

var batchSize = 3;
var batched = orig
    .Select((Value, Index) => new {Value, Index})
    .GroupBy(p => p.Index/batchSize)
    .Select(g => g.Select(p => p.Value).ToList());
于 2012-05-31T03:31:00.677 に答える
5

MoreLinq.Batchを使用する

 var result = inputArray.Batch(n); // n -> batch size

    var inputs = Enumerable.Range(1,10);

    var output = inputs.Batch(3);


    var outputAsArray = inputs.Batch(3).Select(x=>x.ToArray()).ToArray(); //If require as array
于 2012-05-31T03:19:39.740 に答える
3

あなたが欲しいTake()Skip()。これらのメソッドを使用すると、を分割できますIEnumerableConcat()次に、それらを再び一緒に叩くために使用できます。

于 2012-05-31T03:18:42.627 に答える
2

以下のサンプルでは、​​配列をそれぞれ4つのアイテムのグループに分割します。

int[] items = Enumerable.Range(1, 20).ToArray(); // Generate a test array to split
int[][] groupedItems = items
                         .Select((item, index) => index % 4 == 0 ? items.Skip(index).Take(4).ToArray() : null)
                         .Where(group => group != null)
                         .ToArray();
于 2012-05-31T03:42:18.660 に答える
2

これは純粋なLINQではありませんが、一緒に使用することを目的としています。

public static class MyEnumerableExtensions
{
    public static IEnumerable<T[]> Split<T>(this IEnumerable<T> source, int size)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source can't be null.");
        }

        if (size == 0)
        {
            throw new ArgumentOutOfRangeException("Chunk size can't be 0.");
        }

        List<T> result = new List<T>(size);
        foreach (T x in source)
        {
            result.Add(x);
            if (result.Count == size)
            {
                yield return result.ToArray();
                result = new List<T>(size);
            }
        }
    }
}

コードから次のように使用できます。

private void Test()
{
    // Here's your original sequence
    IEnumerable<int> seq = new[] { 1, 2, 3, 4, 5, 6 };

    // Here's the result of splitting into chunks of some length 
    // (here's the chunks length equals 3). 
    // You can manipulate with this sequence further, 
    // like filtering or joining e.t.c.
    var splitted = seq.Split(3);
}
于 2012-05-31T03:19:04.170 に答える
1

次のように簡単です。

static class LinqExtensions
{
    public static IEnumerable<IEnumerable<T>> ToPages<T>(this IEnumerable<T> elements, int pageSize)
    {
        if (elements == null)
            throw new ArgumentNullException("elements");
        if (pageSize <= 0)
            throw new ArgumentOutOfRangeException("pageSize","Must be greater than 0!");

        int i = 0;
        var paged = elements.GroupBy(p => i++ / pageSize);
        return paged;
    }
}
于 2016-09-13T13:43:14.687 に答える