Split C# collection into equal parts, maintaining sort

前端 未结 8 1778
野的像风
野的像风 2021-02-19 18:31

I am trying to split a collection into multiple collections while maintaining a sort I have on the collection. I have tried using the following extension method, but it breaks

8条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 19:06

        public static IEnumerable> Split(this IEnumerable list, int parts)
        {
            int nGroups = (int)Math.Ceiling(list.Count() / (double)parts);
    
            var groups = Enumerable.Range(0, nGroups);
    
            return groups.Select(g => list.Skip(g * parts).Take(parts));
        }
    

提交回复
热议问题