how to split ObservableCollection

放肆的年华 提交于 2021-02-05 08:15:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!