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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:51:41+00:00 2026-06-10T12:51:41+00:00

I am dealing with trying to chunk up items in a custom collection class

  • 0

I am dealing with trying to chunk up items in a custom collection class that implements IEnumerable (and ICollection) in C# 2.0. Let’s say, for example, that I only want 1000 items at a time and I have 3005 items in my collection. I’ve got a working solution that I demonstrate below, but it seems so primitive that I figure there has to be a better way to do this.

Here’s what I have (for example’s sake, I’m using C# 3.0’s Enumerable and var, just replace those references with a custom class in your mind):

var items = Enumerable.Range(0, 3005).ToList();
int count = items.Count();
int currentCount = 0, limit = 0, iteration = 1;

List<int> temp = new List<int>();

while (currentCount < count)
{
    limit = count - currentCount;

    if (limit > 1000)
    {
        limit = 1000 * iteration;
    }
    else
    {
        limit += 1000 * (iteration - 1);
    }
    for (int i = currentCount; i < limit; i++)
    {
        temp.Add(items[i]);
    }

    //do something with temp

    currentCount += temp.Count;
    iteration++;
    temp.Clear();
}

Can anyone suggest a more elegant way of doing this in C# 2.0? I know if this project was from the past 5 years I could use Linq (as demonstrated here and here). I know my method will work, but I’d rather not have my name associated with such ugly (in my opinion) code.

Thanks.

  • 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-10T12:51:43+00:00Added an answer on June 10, 2026 at 12:51 pm

    Firstly . yield is your friend here, and it was introduced with 2.0. Consider:

    public static IEnumerable<List<T>> Chunk<T>(IEnumerable<T> source, int chunkSize)
    {
      List<T> list = new List<T>(chunkSize);
      foreach(T item in source)
      {
        list.Add(item);
        if(list.Count == chunkSize)
        {
          yield return list;
          list = new List<T>(chunkSize);
        }
      }
      //don't forget the last one!
      if(list.Count != 0)
        yield return list;
    }
    

    Then we’re flexible in type and size, so it’s nicely reusable. The only that being restricted to 2.0 means, is that we can’t make it an extension method.

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

Sidebar

Related Questions

I was dealing with hibernate, trying to figure out the run-time class behind proxied
I'm dealing with an undocumented API that I'm trying to do a bit of
I'm trying to extend ScriptManager to simplify dealing with resources that have multiple resource
I'm dealing with an _NSDictionary that I'm trying to parse. I'm using this code
Dealing with some legacy code and in trying to get a poorly designed database
I am trying to configure tomcat according to Dealing with "java.lang.OutOfMemoryError: PermGen space" error
I'm trying to implement the Miller test in Haskell (Not Miller-Rabin.) I'm dealing with
I am trying to do some html parsing. I am dealing with some very
When dealing with a collection of key/value pairs is there any difference between using
i'm really new to dealing with CGContexts and i'm just trying to get my

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.