Split List into Sublists with LINQ

前端 未结 30 2390
灰色年华
灰色年华 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:57

    Using modular partitioning:

    public IEnumerable> Split(IEnumerable input, int chunkSize)
    {
        var chunks = (int)Math.Ceiling((double)input.Count() / (double)chunkSize);
        return Enumerable.Range(0, chunks).Select(id => input.Where(s => s.GetHashCode() % chunks == id));
    }
    

提交回复
热议问题