How to split an ItemCollection into 2 at a specified index?

107 Views Asked by At

This seems like a very basic thing to do, but I can't find any methods to do this. I've checked Intellisense and searched Google with no luck.

I have an ItemCollection with ~30 items in it. I'm trying to get the first 14 items to remain in the original ItemCollection and the latter 16 (or however many) moved to a new ItemCollection.

How can I do this? myVar.CopyTo() would be ok, but there is no parameter for the number of items to copy, and it only accepts an Array for output. Looping over myVar.RemoveAt() seems expensive. Is there a built-in method? Is it possible with Linq?

2

There are 2 best solutions below

1
Sujeevan Jeyabalasundaram On

If you could convert the ItemCollection into an ArrayList; then you can try this:

arraylist.RemoveRange( x, y );

This removes y elements starting at index x.

Finally you could convert the ArrayList back to an ItemCollection.

This is useful if you have too many items in the collection.

0
Danny Beckett On

This is what I've ended up doing within my Print class:

var Data = ...; // The original ItemCollection
var DataExcess = new DataGrid().Items; // It isn't possible to use new ItemCollection();

for(var i = 0; i < Data.Count; i++) {
    if(i > 13) {
        DataExcess.Add(Data[i]);
        continue;
    }

    // Otherwise print the row to the page using e.Graphics.DrawString(..)
}

if(DataExcess.Count > 0) {
    new Print(Some, Parameters, Here, ..., DataExcess).Print();
}