问题
i have ObservableCollection with 100 records.
now i want to get split that collection in 10 new collection each having 10 records.
it means 1 collection = 100 records (10 collection = 10 records) = 1 collection
any help will be apricited.
回答1:
Using Linq
var collection=new ObservableCollection<int>(Enumerable.Range(1,100));
collection.Aggregate(new ObservableCollection<ObservableCollection<int>>(),
(x,i)=>{
if (!x.Any() || x.Last().Count()==10) x.Add(new ObservableCollection<int>());
x.Last().Add(i);
return x;
}
);
or
ObservableCollection<ObservableCollection<T>> Split(ObservableCollection<T> collection,int splitBy=10) {
var result=collection
.Select((x,i)=>new {index=i,item=x})
.GroupBy(x=>x.index/splitBy,x=>x.item)
.Select(g=>new ObservableCollection<T>(g));
return new ObservableCollection<ObservableCollection<T>(result);
}
来源:https://stackoverflow.com/questions/6937948/how-to-split-observablecollection