In my C # code, I need to evaluate two non-zero variables. I developed an if-else if statement set, but in my opinion it looks ugly and a little too messy, even if it is correct.
I looked at 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 result?
Update: I filled out the 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 ask is a general one that I would like to draw attention to: the choice, not the code used after the selection.
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); } }
user4864716
source share