我的排序在Linq中未正确返回 [英] My sorting is not returning correctly in Linq

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

问题描述

我正在尝试确定为什么我的排序不起作用.我没有收到错误,方法只返回未排序的列表.

I am trying to determine 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;

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

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