WP7 ObservableCollection排序

时间:2012-03-26 09:43:07

标签: c# windows-phone-7 sorting observablecollection

我有一个集合:

public ObservableCollection<Shops> ShopList { get; set; }

然后我把4个元素放入其中(它显示正确),我尝试对它们进行排序

ShopList.Clear();
var orderedlist = ShopList.OrderBy(k => k.Name);
foreach (Shops s in orderedlist)
    ShopList.Add((Shops)s);

ShopList现在有4个null元素。为什么这样,以及如何纠正代码? 感谢

1 个答案:

答案 0 :(得分:2)

这是正确的顺序:

var orderedlist = ShopList.OrderBy(k => k.Name);
ShopList.Clear();
foreach (Shops s in orderedlist)
    ShopList.Add((Shops)s);