我正在使用实体框架数据库,我想从一个表中填充我的dropdownlist,但顺序是正确的。
现在我正在使用代码:

var databaseList = from p in db.TECHNICAL_SKILLS
                   where p.skill_type == "Database"
                   select new EmployeeTechnicalSkillInfo
                       {
                           TechnicalSkillId = p.technical_skill_id,
                           SkillType = p.skill_type,
                           SkillName = p.skill_name
                       };

        List<object> sDataValue = new List<object>();
        sDataValue.Add("- Select -");
        sDataValue.Remove("Other");

        foreach (var vData in databaseList)
        {
            sDataValue.Add(vData.SkillName);
        }

        sDataValue.Add("Other");

        DropDownListDB.DataSource = sDataValue.ToArray();
        DropDownListDB.DataBind();

这就是解决办法
    var databaseList = from p in db.TECHNICAL_SKILLS
           where p.skill_type == "Database"
           orderby p.skill_name != "Other" descending, p.skill_name
           select new EmployeeTechnicalSkillInfo
               {
                   TechnicalSkillId = p.technical_skill_id,
                   SkillType = p.skill_type,
                   SkillName = p.skill_name
               };

最佳答案

您可以尝试使用布尔值播放orderby

orderby p.skill_name != "Other" descending, p.skill_name

在您的代码中=>
var databaseList = from p in db.TECHNICAL_SKILLS
               where p.skill_type == "Database"
               orderby p.skill_name != "Other" descending, p.skill_name
               select new EmployeeTechnicalSkillInfo
                   {
                       TechnicalSkillId = p.technical_skill_id,
                       SkillType = p.skill_type,
                       SkillName = p.skill_name
                   };

    List<object> sDataValue = new List<object>();
    sDataValue.Add("- Select -");

    foreach (var vData in databaseList)
    {
        sDataValue.Add(vData.SkillName);
    }

    DropDownListDB.DataSource = sDataValue.ToArray();
    DropDownListDB.DataBind();

关于c# - 排序下拉列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15656191/

10-13 06:46