Pat*_*ick 3 c# wpf range observablecollection
像(伪代码)的东西
ObservableCollection<TheClass> ob1 = new ObservableCollection<TheClass>();
ob1.Add(...);
ob1.Add(...);
ob1.Add(...);
ob1.Add(...);
ObservableCollection<TheClass> ob2;
ob2 = ob1.Range(0, 2);
Run Code Online (Sandbox Code Playgroud)
考虑到两个集合都可以包含大量数据.
谢谢
ObservableCollection<TheClass> ob1 = new ObservableCollection<TheClass>();
ob1.Add(...);
ob1.Add(...);
ob1.Add(...);
ob1.Add(...);
ObservableCollection<TheClass> ob2;
// ob2 = ob1.Range(0, 2);
ob2 = new ObservableCollection(ob1.Skip(0).Take(2));
Run Code Online (Sandbox Code Playgroud)
现在如果你真的坚持这种.Range方法,你可以自己写一个扩展:
public static class ObservableCollectionExtensions
{
public static ObservableCollection<T> Range(this ObservableCollection<T> sequence, int start, int count)
{
return new ObservableCollection(sequence.Skip(start).Take(count));
}
}
Run Code Online (Sandbox Code Playgroud)
现在你的代码应该编译:
ObservableCollection<TheClass> ob1 = new ObservableCollection<TheClass>();
ob1.Add(...);
ob1.Add(...);
ob1.Add(...);
ob1.Add(...);
ObservableCollection<TheClass> ob2;
ob2 = ob1.Range(0, 2);
Run Code Online (Sandbox Code Playgroud)