Split List into Sublists with LINQ

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

    For anyone interested in a packaged/maintained solution, the MoreLINQ library provides the Batch extension method which matches your requested behavior:

    IEnumerable source = "Example string";
    IEnumerable> chunksOfThreeChars = source.Batch(3);
    

    The Batch implementation is similar to Cameron MacFarland's answer, with the addition of an overload for transforming the chunk/batch before returning, and performs quite well.

提交回复
热议问题