2012-10-02 52 views
0

我已經得到了我的品牌的完整列表,以顯示在我的DropDownBox中,但似乎沒有順序(只是它們如何輸入到數據庫中),我需要按字母順序對它們進行排序。排序IList

但它看起來不像我可以使用.Sort();在IListILists似乎沒有任何類似的東西,所以我有點損失,我試圖將IList轉換爲List,然後使用List.Sort()方法,但我沒有運氣與此作爲它只是回來unsorted再次:

public void BrandListRetrieve() 
{ 
    var factory = new BrandFactory(); 
    var customBool1State = 
      factory.ByCustomBoolean1(false, CoreHttpModule.Session); 

    if (customBool1State != null) 
    { 
     var brandDropDown = CoreHttpModule 
           .Session 
           .CreateCriteria(typeof(Brand)).List<Brand>(); 

     foreach (Brand brand in brandDropDown) 
     { 
      this.Items.Add(brand.Name); 
     } 

     if (this.Items.Count < 0) 
     { 
      this.Items.Insert(0, new ListItem("Hello World", "Hello World")); 
     } 

     var brandList = brandDropDown as List<string>; 

     if (brandList != null) 
      brandList.Sort(); 
    } 
} 
+0

什麼是'this.Items'?看起來你正在排序一個列表('brandDropDown')並期待排序結果出現在第二個列表('this.Items')中。 – Dennis

+0

這是列表控件中項目的集合:P –

+0

那麼,爲什麼應該在列表控件中的項目集合被排序後,在對另一個列表中的項目集合進行填充之後,當您對某個*另一個*列表進行排序時呢?此外,「this.Items」是什麼類型? – Dennis

回答

3

你應該試試這個;

foreach (Brand brand in brandDropDown.OrderBy(b => b.Name)) 

可以肯定REMOVE從以下幾行代碼;

 var brandList = brandDropDown as List<string>; 

     if (brandList != null) 
      brandList.Sort(); 
+0

嘿薩傑,那只是我亂搞,試圖讓它排序正確與失敗的嘗試。你的第一行代碼不代表 foreach(品牌brandDropDown) 這個.OrderBy(b => b.Name); } 還使用原來的代碼,據我所知,它不包含排序依據的方法 –

+3

不,他的意思,他:-) – SeToY

+0

類型不,我不是那個意思,你wan't之前訂購的保藏你將它添加到下拉列表中。這就是我的代碼行。 – saj