krisal
krisal

Reputation: 631

Sorting a dropdownlist

I'm working with entity framework database and I want to populate my dropdownlist from a table but in right order.

Right now i'm using the code:

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

This is the Solution

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

Upvotes: 2

Views: 138

Answers (4)

binard
binard

Reputation: 1784

You can try play with orderby with boolean value

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

In your code =>

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

Upvotes: 0

Manish Mishra
Manish Mishra

Reputation: 12375

Just do this:

Remove -Select- and Other from databaseList like this:

var databaseList = from p in db.TECHNICAL_SKILLS
                   where p.skill_type == "Database"
                     && p.skill_name.ToLower() !="other"
                     && p.skill_name.ToLower() !="-select-"

                   select new EmployeeTechnicalSkillInfo
                       {
                           TechnicalSkillId = p.technical_skill_id,
                           SkillType = p.skill_type,
                           SkillName = p.skill_name
                       };

and then do this:

DropDownListDB.DataSource = sDataValue.ToArray().OrderBy(s=>s);
DropDownListDB.Items.Insert(0, "-Select-");
DropDownListDB.Items.Add("Other");

if you must bring -Select- and Other field from database, binding them to the DropDownList and then removing them and again inserting them at appropriate location is little expensive.

You simply don't pick those two rows from the database. it will be more efficient.

Upvotes: 1

bash.d
bash.d

Reputation: 13207

Remove the entry -Select- from the list and append it as first item to your List<object>:

List<object> sDataValue = new List<object>();
sDataValue.Add("-Select-");
foreach (var vData in databaseList){}

would be the easiest way, I assume.

EDIT

then you will use if/else, maybe and

foreach (var vData in databaseList){} //fill your list
sDataValue.Add("-Other-");

Upvotes: 3

Hans Kesting
Hans Kesting

Reputation: 39284

You could add a "section id" to your database: 0 for "-Select-", 2 for "Other", 1 for the rest. Then sort on 1) section ID, 2) name.

But it would be best not to store that "-Select-" at all (it doesn't represent a valid value) and add it in the correct spot after the databind.

Upvotes: 1

Related Questions