Split a List into smaller lists of N size

前端 未结 17 1786
后悔当初
后悔当初 2020-11-22 16:55

I am attempting to split a list into a series of smaller lists.

My Problem: My function to split lists doesn\'t split them into lists of the correct

17条回答
  •  萌比男神i
    2020-11-22 17:54

    public static IEnumerable> SplitIntoSets
        (this IEnumerable source, int itemsPerSet) 
    {
        var sourceList = source as List ?? source.ToList();
        for (var index = 0; index < sourceList.Count; index += itemsPerSet)
        {
            yield return sourceList.Skip(index).Take(itemsPerSet);
        }
    }
    

提交回复
热议问题