Grouping lists into groups of X items per group

后端 未结 5 741
一个人的身影
一个人的身影 2020-12-09 22:54

I\'m having a problem knowing the best way to make a method to group a list of items into groups of (for example) no more than 3 items. I\'ve created the method below, but w

5条回答
  •  一整个雨季
    2020-12-09 23:30

    I think you are looking for something like this:

    return source.Select((x, idx) => new { x, idx })
          .GroupBy(x => x.idx / itemsPerGroup)
          .Select(g => g.Select(a => a.x));
    

    You need to change your return type as IEnumerable>

提交回复
热议问题