Note that there are some explanatory texts on larger screens.

plurals
  1. POChunk IEnumerable/ICollection Class C# 2.0
    primarykey
    data
    text
    <p>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.</p> <p>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):<br/></p> <pre><code>var items = Enumerable.Range(0, 3005).ToList(); int count = items.Count(); int currentCount = 0, limit = 0, iteration = 1; List&lt;int&gt; temp = new List&lt;int&gt;(); while (currentCount &lt; count) { limit = count - currentCount; if (limit &gt; 1000) { limit = 1000 * iteration; } else { limit += 1000 * (iteration - 1); } for (int i = currentCount; i &lt; limit; i++) { temp.Add(items[i]); } //do something with temp currentCount += temp.Count; iteration++; temp.Clear(); } </code></pre> <p>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 <a href="https://stackoverflow.com/questions/1349491/how-can-i-split-an-ienumerablestring-into-groups-of-ienumerablestring">here</a> and <a href="https://stackoverflow.com/questions/6849315/how-do-i-segment-the-elements-iterated-over-in-a-foreach-loop">here</a>). I know my method will work, but I'd rather not have my name associated with such ugly (in my opinion) code.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload