我的排序没有正确返回 [英] My sorting is not returning correctly

查看:142
本文介绍了我的排序没有正确返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指出我的排序为什么不工作?我没有得到错误,方法只是返回列表没有排序。

  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;

字典< 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));

返回customerQuery;
}


解决方案

  IEnumerable< Customer> result = orderings [sortExpression](customerQuery.Skip(startRows).Take(maxRows)); 
返回结果;


Can someone please point out why my sorting is not working? I'm not getting an error, method just returns list not sorted.

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;
}

解决方案

You are not returning the sorted collection.

IEnumerable<Customer> result = orderings[sortExpression](customerQuery.Skip(startRows).Take(maxRows));
return result;

这篇关于我的排序没有正确返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆