Recentemente sto cercando di aggiornare il mio progetto MVC al progetto dotnetCore, mentre qualcosa è andato storto in BaseService ..
il mio precedente codice MVC che funziona bene:
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();
}
}
e il codice 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;
}
e che strano è che il codice funzioni bene solo ordinando o semplicemente impaginando;
ma in entrambe le condizioni (paginazione e ordinamento), il secondo pagedata andava sempre male (prima pagedata sempre a destra);
Pagina 1:
pagina 2:
Non so perché ma sostituisco l'ultimo 2 codice di linea, i dati vengono bene ...
qualcuno può annunciare il meccanismo?
var list = await tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).ToListAsync();
return list;
come
var list = tempData.ToAsyncEnumerable().Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows);
return await list.ToList();