Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:57:18+00:00 2026-06-17T08:57:18+00:00

I have the following extension method to split a List<T> into a list of

  • 0

I have the following extension method to split a List<T> into a list of List<T>‘s with different chunk sizes, but I’m doubting its efficiency. Anything I can do to improve it or is it fine as is?

public static List<List<T>> Split<T>(this List<T> source, params int[] chunkSizes)
{
    int totalSize = chunkSizes.Sum();
    int sourceCount = source.Count();
    if (totalSize > sourceCount)
    {
        throw new ArgumentException("Sum of chunk sizes is larger than the number of elements in source.", "chunkSizes");
    }

    List<List<T>> listOfLists = new List<List<T>>(chunkSizes.Length);
    int index = 0;
    foreach (int chunkSize in chunkSizes)
    {
        listOfLists.Add(source.GetRange(index, chunkSize));
        index += chunkSize;
    }

    // Get the entire last part if the total size of all the chunks is less than the actual size of the source
    if (totalSize < sourceCount)
    {
        listOfLists.Add(source.GetRange(index, sourceCount - totalSize));
    }

    return listOfLists;
}

Example code usage:

List<int> list = new List<int> { 1,2,4,5,6,7,8,9,10,12,43,23,453,34,23,112,4,23 };
var result =  list.Split(2, 3, 3, 2, 1, 3);
Console.WriteLine(result);

This gets a desired result and a has a final list part with 4 numbers as the total chunk size is 4 less than the size of my list.

I’m especially doubtful of the GetRange part as I fear this is just enumerating the same source over and over…

EDIT: I think I know a way to enumerate the source once: Just do a foreach on the source itself and keep checking if the number of iterated elements is the same as the current chunksize. If so, add the new list and go to the next chunksize. Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:57:18+00:00Added an answer on June 17, 2026 at 8:57 am

    There is no performance problem with this code. GetRange is documented to be O(chunkSize), and this is also easy to deduce since one of the most important properties of List<T> is exactly that it allows O(1) indexing.

    That said, you could write a more LINQ-y version of the code like this:

    var rangeStart = 0;
    var ranges = chunkSizes.Select(n => Tuple.Create((rangeStart += n) - n, n))
                           .ToArray();
    var lists = ranges.Select(r => source.GetRange(r.Item1, r.Item2)).ToList();
    
    if (rangeStart < source.Count) {
        lists.Last().AddRange(source.Skip(rangeStart));
    }
    
    return lists;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following extension method that takes a List and converts it to
I have the following extension method: internal static string ReadLine(this DataReader stream) { Task<string>
I have the following extension method (which is invalid and does not compile ATM):
I have the following extension method public static class ListExtensions { public static IEnumerable<T>
I have following extension method written: static public IQueryable<OutboundPattern> ByClientID(this IQueryable<OutboundPattern> qry, int clientID)
I have the following extension method to add the elements in one collection to
I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
I have the following method, and I want to know if there is anything
I have the following extension method which asserts that a property (Id) contains a
In some legacy code I have seen the following extension method to facilitate adding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.