1

我已经在存储过程中完成了分页排序。但是现在,我需要在 LINQ 中对以下对象执行相同的操作。在以下场景中实现排序和分页的最易读的 LINQ 查询是什么?

代码

static  List<Employee> GetMyData(int pageIndex, int pageSize, out int itemCount, string sortField, string sortDirection )
{

    Employee e1= new Employee(){EmpID = 1, EmpName = "E1"};
    Employee e2= new Employee(){EmpID = 2, EmpName = "E2"};
    Employee e3= new Employee(){EmpID = 3, EmpName = "E3"};
    Employee e4= new Employee(){EmpID = 4, EmpName = "E4"};
    Employee e5= new Employee(){EmpID = 5, EmpName = "E5"};
    Employee e6= new Employee(){EmpID = 6, EmpName = "E6"};

    List<Employee> employees = new List<Employee>();
    employees.Add(e1);
    employees.Add(e2);
    employees.Add(e3);
    employees.Add(e4);
    employees.Add(e5);
    employees.Add(e6);

    itemCount = employees.Count;

    //Get subset of employees based on paging and sorting parameters
    //Sorting can be based on EmpID or EmpName
    List<Employee> currentPageEmployees = null;
    return currentPageEmployees;
}

调用方法

        int count = 0;
        List<Employee> e = GetMyData(0, 2, out count, "EmpID", "descending");
4

5 回答 5

2

按任何字段排序只能使用动态 linq http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-库.aspx

排序:

if (sortDirection == "ASC") 
    employees = employees.OrderBy(sortField).ToList();
else 
   employees = employees.OrderByDesceding(sortField).ToList();

分页:

var currentPageEmployees = sorted.Skip(pageindex*pageSize).Take(pageSize)

编辑:使用 sortField 和 sortDirection 更新代码

于 2013-08-01T06:26:47.353 回答
2

您可以使用自定义选择器来解析sortFieldEmployee 类型的适当属性。然后对于排序方向,我假设sortDirection是升序还是降序 - 如果是,那么可能 abool更好?

鉴于这是一个示例:

var selector = new Func<Employee, object>(e => e.GetType().GetProperty(sortField).GetValue(e, null));
var query = sortDirection.Equals("descending", StringComparison.OrdinalIgnoreCase)
                ? employees.OrderByDescending(selector)
                : employees.OrderBy(selector);

List<Employee> currentPageEmployees = query
    .Skip(pageIndex * pageSize)
    .Take(pageSize)
    .ToList();

有 Dynamic Linq 扩展可能会使事情更具可读性。

编辑:删除铸造

于 2013-08-01T06:45:23.077 回答
1

因为sorting您可以使用orderbylinq 方法指定要对其应用排序的参数。

 var result = employess.OrderBy(x=>x.Name);
 ar result = employess.OrderByDescending(x=>x.Name);

因为paging您可以像这样协作使用SKipTake方法

 var queryResultPage = employess
                      .Skip(numberOfObjectsPerPage * pageNumber)
                      .Take(numberOfObjectsPerPage);
于 2013-08-01T06:29:48.400 回答
0

尝试这个

List<Employee> currentPageEmployees = employees.OrderBy(p => ((sortField == "EmpID") ? p.EmpID : (((sortField == "EmpName") ? p.EmpName : (p.EmpID))))).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

参考

于 2013-08-01T06:57:24.203 回答
0

其他人使用Skipand Take,这没关系,但我们也可以使用Where

enum SortDirection {
    Ascending,
    Descending
}

static  List<Employee> GetMyData(int pageIndex, int pageSize, out int itemCount, string sortField, SortDirection sortDirection )
{

    //.........
    //.........  

    int startIndex = pageIndex * pageSize; //pageIndex is Zero-based
    int endIndex = startIndex + pageSize;

    //Get subset of employees based on paging and sorting parameters
    //Sorting can be based on EmpID or EmpName <-- this is why I can use the ? below
    List<Employee> currentPageEmployees = 
          sortDirection == SortDirection.Ascending ? 
          employees.OrderBy(e=>sortField=="EmpID" ? e.EmpID : e.EmpName)
                   .Where((e,i)=>i>=startIndex&&i<endIndex)
          :
          employees.OrderByDescending(e=>sortField=="EmpID" ? e.EmpID : e.EmpName)
                   .Where((e,i)=>i>=startIndex&&i<endIndex);
    return currentPageEmployees;
}
于 2013-08-01T07:09:02.527 回答