Yesterday i was playing with jQGrid plugin and ASP.NET. Everything turned out well, my grid is working now, but i have two methods, that make my code smell.
smelly methods:
private IOrderedEnumerable<Employee> GetOrderedEmployees(Column sortColumn, bool ascending)
{
switch (sortColumn)
{
case Column.Name:
{
return GetOrderedEmployees(e => e.Name, ascending);
}
case Column.Salary:
{
return GetOrderedEmployees(e => e.Salary, ascending);
}
default:
{
return GetOrderedEmployees(e => e.ID, ascending);
}
}
}
private IOrderedEnumerable<Employee> GetOrderedEmployees<TSortKey>(Func<Employee, TSortKey> func, bool ascending)
{
return ascending ? Context.Employees.OrderBy(func) : Context.Employees.OrderByDescending(func);
}
I can't find out, how to refactor them properly. It's seems the best solution is to return only lambdas (f.e return e=>e.Name
) in the switch statement, but how can it be done?
Edit - in the switch statement ascending
argument is passed 3 times. Isn't it a duplication?