排序IList

时间:2012-10-02 09:46:12

标签: c#

我有一个完整的品牌列表可以显示在我的DropDownBox中,但似乎没有订单(只是它们如何输入到数据库中),我需要按字母顺序对它们进行排序。 / p>

但它看起来不像我可以使用.Sort();在IListILists似乎没有任何相似之处,所以我有点不知所措,我试图将IList转换为List 1}}然后使用List.Sort()方法,但我没有运气,因为它再次没有被排除:

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();
    }
}

2 个答案:

答案 0 :(得分:2)

你应该试试这个;

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

您当然可以删除代码中的以下行;

        var brandList = brandDropDown as List<string>;

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

答案 1 :(得分:-1)

似乎你需要使用带有Compare方法的brand.Sort()。请尝试阅读本手册http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx