c# - Group, Sort, and then Merge to a Observable Collection? -
i have list of items have "displayorder" property. can either have null or int value. if has int value, has priority , should in 1st group of observable collection. items in 1st group sorted displayorder.
if null, belongs 2nd group, sorted alphabetically.
the 1st group , 2nd group combined main items collection observable collection bind listview.
this current code though worried if there optimal way of doing it.
var mainitemcollection = new observablecollection<mainitemviewmodel>(); var itemswithproperty = new observablecollection<mainitemviewmodel>(); var itemswithnullproperty = new observablecollection<mainitemviewmodel>(); foreach (var item in dataobject.mainitems) { if (item.displayorder == null) itemswithnullproperty.add(new mainitemviewmodel(item)); else itemswithproperty.add(new mainitemviewmodel(item)); } itemswithproperty = new observablecollection<mainitemviewmodel>(itemswithproperty.orderby(c => c.displayorder)); itemswithnullproperty = new observablecollection<mainitemviewmodel>(itemswithnullproperty.orderby(c => c.title)); //add priorities first sorted displayorder 1,2,3,4 foreach (var c in itemswithproperty) { mainitemcollection.add(c); } //add without priority sorted alphabetically foreach (var c in itemswithnullproperty) { mainitemcollection.add(c); }
thank you!
get items displayorder=null & order them title:
itemswithnullproperty=dataobject.mainitems.where(x=>x.displayorder==null).orderby(o=>o.title).tolist();
get items displayorder(all items except above query) & order them displayorder:
itemswithproperty= dataobject.mainitems.except(itemswithnullproperty).orderby(o=>o.displayorder).tolist();
fill data in maincollection:
var allitems = mainitemcollection.concat(itemswithproperty) .concat(itemswithnullproperty) .tolist();
Comments
Post a Comment