开发者

how to split ObservableCollection

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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜