我正在使用以下代码来获取数据以绑定gridview,但是对于性能问题,我只想获得选定的数据行范围,对于第二页,首页11-20说1-10,我该怎么办?修改以下代码
public IQueryable<Employee> SelectEmployees(string SortColumn, string SortOrder, long UserID,---int start,---int end)
{
IUnitOfWork objUow;
var arg = Expression.Parameter(typeof(Employee), "Employee");
var sortProperty = Expression.Property(arg, SortColumn);
var lambda = Expression.Lambda(sortProperty, arg);
var param = Expression.Parameter(typeof(IQueryable<Employee>));
var orderByCall = Expression.Call(typeof(Queryable), SortOrder, new Type[] { typeof(Employee), sortProperty.Type }, new Expression[] { param, lambda });
var orderLambda = Expression.Lambda<Func<IQueryable<Employee>, IQueryable<Employee>>>(orderByCall, param).Compile();
var Ids = objUow.Repository<Users>().Entities.Where((x => x.ID == UserID)).Select(y => y.Id);
return orderLambda(objUow.Repository<Employee>().Entities.Where(x => x.IsDeleted != true && x.Status == 1 && Ids.Contains(x.CheckID)));
}
我尝试过skip()。take()但是它重新出错了。 ......
return orderLambda((objUow.Repository<Employee>().Entities.Where(x => x.IsDeleted != true && x.Status == 1 && Ids.Contains(x.CheckID))).Skip(0).Take(10));
类型&#39; System.NotSupportedException&#39;的例外情况发生在EntityFramework.SqlServer.dll中但未在用户代码中处理
其他信息:方法&#39; Skip&#39;仅支持LINQ to Entities中的排序输入。方法&#39; OrderBy&#39;必须在方法之前调用&#39;跳过&#39;。
我可以做下面的事吗
IQueryable<Employee> Obj= orderLambda(objUow.Repository<Employee>().Entities.Where(x => x.IsDeleted != true && x.InvalidStatus == 1 && shopids.Contains(x.CheckID)));
Obj.Skip(0).Take(10);
答案 0 :(得分:1)
return orderLambda((objUow.Repository<Employee>()
.Entities.Where(x => x.IsDeleted != true && x.Status == 1 && Ids.Contains(x.CheckID)))
.OrderBy(x=> x.CheckID) // you must order the results before skip and take
.Skip(0)
.Take(10));
错误告诉你
修改强>
要按字符串排序,您可能需要System.Linq.Dynamic nuget包和命名空间。然后你可以用OrderBy(SortOrder)替换我的OrderBy 然后就可以了
OrderBy(string.Format("{0} {1}", SortOrder, SortDirection)) // where Sort direction can be "descending" if it's reversed
// replace 0 & 10 as appropriate
答案 1 :(得分:1)
每次查询数据库时,您都需要知道要跳过的记录数。 为此,您应该将页码乘以页面大小。 这是一个例子:
int skipAmount = pageNumber * pageSize;
_repository.Where(a => a.IsDeleted != false)
.OrderBy(a => a.Date)
.Skip(skipAmount)
.Take(pageSize);