Split List into Sublists with LINQ

前端 未结 30 2394
灰色年华
灰色年华 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 07:01

    I find this little snippet does the job quite nicely.

    public static IEnumerable> Chunked(this List source, int chunkSize)
    {
        var offset = 0;
    
        while (offset < source.Count)
        {
            yield return source.GetRange(offset, Math.Min(source.Count - offset, chunkSize));
            offset += chunkSize;
        }
    }
    

提交回复
热议问题