均分list

public static class ListExtension
{
    public static IEnumerable<List<T>> SplitListByNumber<T>(this List<T> locations, int nSize = 30)
    {
        for (int i = 0; i < locations.Count; i += nSize)
        {
            yield return locations.GetRange(i, Math.Min(nSize, locations.Count - i));
        }
    }

    public static List<List<T>> SplitListToNParts<T>(this List<T> list, int parts = 10)
    {
        var i = 0;
        var splits = from item in list
        group item by i++ % parts into part
        select part.ToList();
        return splits?.ToList();
    }
}
posted @ 2020-12-14 22:48  天藐水瓶  阅读(91)  评论(0编辑  收藏  举报