I have a drop down list set to auto post back which needs to return a list of products in the specified order. I thought I could just put that query in its own method and use a parameter to specify the orderby, but I cannot get it to work.
Here is an example:
protected void Show_Products(int item)
{
using (storeDataContext db = new storeDataContext())
{
string query = "";
switch (item)
{
case 1:
query ="x.Name";
break;
case 2:
query = "x.MSRP";
break;
default:
break;
}
var q = db.Items.OrderBy(x=> query).Select(x => x);
foreach(var n in q)
{
Do work
}
}
}
And the drop down list method that gets called on postback:
protected void ddlSortBy_SelectedIndexChanged(object sender, EventArgs e)
{
int value =int.Parse(ddlSortBy.SelectedValue);
Show_Products(value);
}