昨天我在玩 jQGrid 插件和 ASP.NET。一切都很好,我的网格现在可以工作了,但是我有两种方法可以让我的代码有异味。
臭臭方法:
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);
}
我不知道如何正确重构它们。似乎最好的解决方案是return e=>e.Name
在 switch 语句中只返回 lambdas (fe ),但是怎么做呢?
在 switch 语句中,ascending
参数被传递了 3 次。不是重复吗?