如何在指定的索引处将ItemCollection拆分为2?

时间:2015-01-28 11:51:34

标签: c# .net wpf linq itemcollection

这似乎是一件非常基本的事情,但我无法找到任何方法来做到这一点。我已经检查过Intellisense并且没有运气搜索Google。

我有一个ItemCollection,里面有约30个项目。我试图让前14个项目保留在原始ItemCollection中,而后者16个(或多个)移动到新的ItemCollection

我该怎么做? myVar.CopyTo()没问题,但是要复制的项目数没有参数,它只接受Array输出。循环myVar.RemoveAt()似乎很昂贵。有内置方法吗? Linq可以吗?

2 个答案:

答案 0 :(得分:0)

这是我在Print课程中最终完成的事情:

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();
}

答案 1 :(得分:-1)

如果您可以将ItemCollection转换为ArrayList;然后你可以试试这个:

arraylist.RemoveRange( x, y );

这将从索引y开始删除x个元素。

最后,您可以将ArrayList转换回ItemCollection

如果集合中的项目太多,这很有用。

相关问题