Split List into Sublists with LINQ

前端 未结 30 2449
灰色年华
灰色年华 2020-11-21 06:26

Is there any way I can separate a List into several separate lists of SomeObject, using the item index as the delimiter of each s

30条回答
  •  灰色年华
    2020-11-21 06:49

    There is no way to combine all desirable features like full lazyness, no copying, full generality and safety in one solution. The most fundamental reason is that it cannot be guaranteed that the input is not mutated before the chunks are accessed. Assume that we have a function of the following signature:

    public static IEnumerable> Chunk(this IEnumerable source, int chunkSize)
    {
        // Some implementation
    }
    

    Then the following way to use it is problematic:

    var myList = new List()
    {
        1,2,3,4
    };
    var myChunks = myList.Chunk(2);
    myList.RemoveAt(0);
    var firstChunk = myChunks.First();    
    Console.WriteLine("First chunk:" + String.Join(',', firstChunk));
    myList.RemoveAt(0);
    var secondChunk = myChunks.Skip(1).First();
    Console.WriteLine("Second chunk:" + String.Join(',', secondChunk));
    // What outputs do we see for first and second chunk? Probably not what you would expect...
    

    Depending on the specific implementation the code will either fail with a runtime error or produce unintuitive results.

    So, at least one of the properties need to be weakened. If you want a bullet-proof lazy solution you need to restrict your input type to an immutable type and even then it is not straightforward to cover up all use cases. If you have control of the usage, you could, however, still opt for the most general solution, as long as you make sure that it is used in a way that works. Otherwise, you might drop the lazyness and accept some amount of copying.

    In the end, it all depends on your use case and requirements which solution is the best choice for you.

提交回复
热议问题