Recently Im trying to upgrade My MVC project to dotnetCore project, while something went wrong in BaseService..
my former MVC code which works good:
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();
}
}
and the dotnetCore code:
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;
}
and what strange is the code works well in just ordersorting or in just pagination;
but in both conditions(pagination&sorting) , the Second pagedata always went wrong(first pagedata always right);
page1:
page2:
I dont know why but I replace the last 2 line code, the data comes right...
anyone can announce the mechanism?
var list = await tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).ToListAsync();
return list;
as
var list = tempData.ToAsyncEnumerable().Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows);
return await list.ToList();