我在存储过程中进行了分页分页。但是现在,我需要在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");
答案 0 :(得分:2)
按任何字段排序只能使用动态linq http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
进行AFAIK排序:
if (sortDirection == "ASC")
employees = employees.OrderBy(sortField).ToList();
else
employees = employees.OrderByDesceding(sortField).ToList();
寻呼:
var currentPageEmployees = sorted.Skip(pageindex*pageSize).Take(pageSize)
编辑:使用sortField和sortDirection更新代码
答案 1 :(得分:2)
您可以使用自定义选择器将sortField
解析为Employee类型的相应属性。然后,对于排序方向,我假设sortDirection
是升序还是降序 - 如果是,那么bool
可能更好?
鉴于这是一个例子:
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();
动态Linq扩展可能会使事情变得更加可读。
编辑:已删除投射
答案 2 :(得分:1)
对于sorting
,您可以使用linq的orderby
方法指定要应用排序的参数。
var result = employess.OrderBy(x=>x.Name);
ar result = employess.OrderByDescending(x=>x.Name);
paging
您可以像这样使用SKip
和Take
方法
var queryResultPage = employess
.Skip(numberOfObjectsPerPage * pageNumber)
.Take(numberOfObjectsPerPage);
答案 3 :(得分:0)
试试这个
List<Employee> currentPageEmployees = employees.OrderBy(p => ((sortField == "EmpID") ? p.EmpID : (((sortField == "EmpName") ? p.EmpName : (p.EmpID))))).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
<强> refrence 强>
答案 4 :(得分:0)
其他人使用Skip
和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;
}