My scenario is: I have a class called MenuBO using linq, it has a function called SelectAll() that returns a list of the object menu to use as a DataSource for a DropDownList. Before returning it I need to insert in the list a new item to serve as a default value for the DropDownList
Here’s what I tried so far:
public List<menu> SelectAll()
{
using (var db = new SeloQual_AdminEntities(conn))
{
menu vmenu = new menu();
vmenu.cod_menu = 0;
vmenu.cod_menu_pai = null;
vmenu.des_menu = "Select...";
var query = from p in db.menu orderby p.des_menu select p;
var test = query.ToList();
test.Add(vmenu);
return test.OrderBy(x => x.cod_menu).ToList();
}
}
The OrderBy(x => x.cod_menu) does it job, the vmenu item appears on top like I want, but I need that the other items to be ordered by des_menu. I tried something like
return teste.OrderBy(x => x.cod_menu).ThenBy(y => y.des_menu).ToList();
But didn’t work of course, so I some need help to order only the elements after the first one
I think what you really want is the following code:
This solves your problem elegantly by simply adding the default menu item first and adding the already sorted menu items afterwards in the order defined in the query.