Split a List into smaller lists of N size

前端 未结 17 1815
后悔当初
后悔当初 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条回答
  •  暖寄归人
    2020-11-22 17:49

    public static List> SplitList(List locations, int nSize=30)  
    {        
        var list = new List>(); 
    
        for (int i = 0; i < locations.Count; i += nSize) 
        { 
            list.Add(locations.GetRange(i, Math.Min(nSize, locations.Count - i))); 
        } 
    
        return list; 
    } 
    

    Generic version:

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

提交回复
热议问题