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 8935107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:55:17+00:00 2026-06-15T09:55:17+00:00

I want to take an IEnumerable<T> and split it up into fixed-sized chunks. I

  • 0

I want to take an IEnumerable<T> and split it up into fixed-sized chunks.

I have this, but it seems inelegant due to all the list creation/copying:

private static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> items, int partitionSize)
{
    List<T> partition = new List<T>(partitionSize);
    foreach (T item in items)
    {
        partition.Add(item);
        if (partition.Count == partitionSize)
        {
            yield return partition;
            partition = new List<T>(partitionSize);
        }
    }
    // Cope with items.Count % partitionSize != 0
    if (partition.Count > 0) yield return partition;
}

Is there something more idiomatic?

EDIT: Although this has been marked as a duplicate of Divide array into an array of subsequence array it is not – that question deals with splitting an array, whereas this is about IEnumerable<T>. In addition that question requires that the last subsequence is padded. The two questions are closely related but aren’t the same.

  • 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-15T09:55:18+00:00Added an answer on June 15, 2026 at 9:55 am

    You could try to implement Batch method mentioned above on your own like this:

        static class MyLinqExtensions 
        { 
            public static IEnumerable<IEnumerable<T>> Batch<T>( 
                this IEnumerable<T> source, int batchSize) 
            { 
                using (var enumerator = source.GetEnumerator()) 
                    while (enumerator.MoveNext()) 
                        yield return YieldBatchElements(enumerator, batchSize - 1); 
            } 
    
            private static IEnumerable<T> YieldBatchElements<T>( 
                IEnumerator<T> source, int batchSize) 
            { 
                yield return source.Current; 
                for (int i = 0; i < batchSize && source.MoveNext(); i++) 
                    yield return source.Current; 
            } 
        }
    

    I’ve grabbed this code from http://blogs.msdn.com/b/pfxteam/archive/2012/11/16/plinq-and-int32-maxvalue.aspx.

    UPDATE: Please note, that this implementation not only lazily evaluates batches but also items inside batches, which means it will only produce correct results when batch is enumerated only after all previous batches were enumerated. For example:

    public static void Main(string[] args)
    {
        var xs = Enumerable.Range(1, 20);
        Print(xs.Batch(5).Skip(1)); // should skip first batch with 5 elements
    }
    
    public static void Print<T>(IEnumerable<IEnumerable<T>> batches)
    {
        foreach (var batch in batches)
        {
            Console.WriteLine($"[{string.Join(", ", batch)}]");
        }
    }
    

    will output:

    [2, 3, 4, 5, 6] //only first element is skipped.
    [7, 8, 9, 10, 11]
    [12, 13, 14, 15, 16]
    [17, 18, 19, 20]
    

    So, if you use case assumes batching when batches are sequentially evaluated, then lazy solution above will work, otherwise if you can’t guarantee strictly sequential batch processing (e.g. when you want to process batches in parallel), you will probably need a solution which eagerly enumerates batch content, similar to one mentioned in the question above or in the MoreLINQ

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LINQ query which returns IEnumerable<List<int>> but i want to return only
I want to take advantage of DateTimePicker 's date validation, but the calendar seemed
In one of my process I have this SQL query that take 10-20% of
I want to take a list of links and split them with alternating joins.
Edit: I have tried the Take/Skip method but I get the following error: Cannot
I have string[] pkgratio= 1:2:6.Split(':'); var items = pkgratio.OrderByDescending(x => x); I want to
I've this simple Entities from DB tables But i want my POCO classes to
I want to take a math expression that takes variables and print (assign to
I want to take backup or restore of my oracle database through .Net code.
I want to take an expression in tcl and convert its _ to <

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.