In my C# code, I need to evaluate two non-null variables. I worked out a set of if-else if statements, but in my mind it looks ugly and a little bit too sloppy, even if it is correct.
I looked in the MSDN Library and saw only examples for selection based on a single variable.
Is there a cleaner and more compact way to achieve the same outcome?
Update: I filled in code to provide more context. Looking at this more, perhaps I can manipulate the linq query directly based on the parameters. However, the question I pose is the generic one that I would like to focus on: the selection rather than the code used after the selection is made.
public ActionResult Index(string searchBy, string orderBy, string orderDir)
{
var query = fca.GetResultsByFilter(searchBy);
if (orderBy == "Campus" && orderDir == "Asc")
{
query = query = query.OrderBy(s => s.Campus).ThenBy(s => s.Student_Name);
}
else if (orderBy == "Campus" && orderDir == "Desc")
{
query = query.OrderByDescending(s => s.Campus);
}
else if (orderBy == "Student Name" && orderDir == "Asc")
{
query = query = query.OrderBy(s => s.Student_Name);
}
else if (orderBy == "Student Name" && orderDir == "Desc")
{
query = query.OrderByDescending(s => s.Student_Name);
}
else if (orderBy == "Course Count" && orderDir == "Asc")
{
query = query.OrderBy(s => s.Course_Count);
}
else if (orderBy == "Course Count" && orderDir == "Desc")
{
query = query.OrderByDescending(s => s.Course_Count);
}
}