Недавно я пытаюсь обновить Мой проект MVC до проекта dotnetCore, хотя что-то пошло не так в BaseService ..
мой прежний MVC код, который работает хорошо:
public List<TEntity> FindList(Expression<Func<TEntity, bool>> predicate, Pagination pagination)
{
using (var ent = new Entities())// System.Data.Entity.DbContext
{
bool isAsc = pagination.sord.ToLower() == "asc";
string[] order = pagination.sidx.Split(',');
MethodCallExpression resultExp = null;
var tempData = ent.Set<TEntity>().Where(predicate);
foreach (string item in order)
{
string orderPart = item;
orderPart = Regex.Replace(orderPart, @"\s+", " ");
string[] orderArry = orderPart.Split(' ');
string orderField = orderArry[0];
if (orderArry.Length == 2)
{
isAsc = orderArry[1].ToUpper() == "ASC";
}
var parameter = Expression.Parameter(typeof(TEntity), "t");
var property = typeof(TEntity).GetProperty(orderField);
if (property != null)
{
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
}
}
if (resultExp != null) tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
pagination.records = tempData.Count();
tempData = tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).AsQueryable();
return tempData.ToList();
}
}
и dotnetCore код:
public async Task<List<TEntity>> FindList(Expression<Func<TEntity, bool>> predicate, Pagination pagination)
{
bool isAsc = pagination.sord.ToLower() == "asc";
string[] order = pagination.sidx.Split(',');
MethodCallExpression resultExp = null;
var tempData = DbSet.Where(predicate);//a instance of Microsoft.EntityFrameworkCore.DbSet<TEntity>
foreach (string item in order)
{
string orderPart = item;
orderPart = Regex.Replace(orderPart, @"\s+", " ");
string[] orderArry = orderPart.Split(' ');
string orderField = orderArry[0];
if (orderArry.Length == 2)
{
isAsc = orderArry[1].ToUpper() == "ASC";
}
var parameter = Expression.Parameter(typeof(TEntity), "t");
var property = typeof(TEntity).GetProperty(orderField);
if (property != null)
{
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
}
}
if (resultExp != null) tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
pagination.records = tempData.Count();
var list = await tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).ToListAsync();
return list;
}
и что странно, код работает хорошо только при сортировке заказов или при нумерации страниц;
, но в обоих условиях (разбиение на страницы и сортировка), Second pagedata всегда шел не так (первая pagedata всегда правильная);
page1:

стр.2:
