有人可以指出为什么我的排序不起作用吗?我没有收到错误,方法只返回未排序的列表。
public IEnumerable<Customer> GetSubsetOfEmployees(int startRows, int maxRows, string sortExpression)
{
NorthwindEntities ndc = new NorthwindEntities();
var customerQuery =
from c in ndc.Customers
//https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
select c;
if (string.IsNullOrEmpty(sortExpression)) sortExpression = "CustomerID";
Dictionary<string, Func<IEnumerable<Customer>, IEnumerable<Customer>>> orderings = new Dictionary<string, Func<IEnumerable<Customer>, IEnumerable<Customer>>>()
{
{ "CustomerID", x1 => x1.OrderBy(x => x.CustomerID) },
{ "CompanyName", x1 => x1.OrderBy(x => x.CompanyName) },
{ "CustomerID DESC", x1 => x1.OrderByDescending(x => x.CustomerID) },
};
orderings[sortExpression](customerQuery.Skip(startRows).Take(maxRows));
return customerQuery;
}
答案 0 :(得分:2)
您没有返回已排序的集合。
IEnumerable<Customer> result = orderings[sortExpression](customerQuery.Skip(startRows).Take(maxRows));
return result;