通过可空类属性和ThenBy对ObservableCollection进行排序

时间:2013-05-18 07:31:17

标签: c# sorting ienumerable observablecollection

我正在尝试对包含以下类

的ObservabkeCollection对象进行排序
 public Favourites(string title=null, string uri=null, string folder=null, bool delete=false)
 {
    this.pageTitle = title;
    this.pageURI = uri;
    this.folder = folder;
    this.deleteEnabled = delete;
  }

我正在尝试按文件夹排序,然后按uri排序。虽然uri和title可以为null,但在我的情况下,我总是给它们分配一些非空的东西。但是,folder属性可以为null。

我要排序的代码

    private void sortCollectionFolderFirst()
    {
        IEnumerable<Favourites> sort;
        ObservableCollection<Favourites> tempSortedFavourites = new ObservableCollection<Favourites>();
        tempFavs.Clear();
        tempFavs = settings.FavouritesSetting;

        sort = tempFavs.OrderByDescending(item => item.Folder).ThenBy(item => item.PageURI);

        foreach (var item in sort)
        {
            tempSortedFavourites.Add(item);
        }

        settings.FavouritesSetting = tempSortedFavourites;            
    }

当课程项目

时,排序很好
this.pageTitle = sometitle;
this.pageURI = someuri;
this.folder = null;
this.deleteEnabled = false or true;

sort在遇到具有以下内容的项目

时返回空
this.pageTitle = sometitle;
this.pageURI = someuri;
this.folder = somefoldername;
this.deleteEnabled = false or true;

为什么?

0 个答案:

没有答案