Entity Framework Core错误的页面数据

时间:2019-02-14 02:05:02

标签: c# entity-framework .net-core entity-framework-core

最近,我试图将我的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;
}

代码奇怪的是,它在排序或分页中都能很好地工作;

但是在两种情况下(分页和排序),第二页面数据总是出错(第一页面数据总是正确);

第1页:

page1

第2页:

page2

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但是我替换了最后两行代码,数据正确...

任何人都可以宣布该机制吗?

        var list = await tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).ToListAsync();
        return list;

        var list = tempData.ToAsyncEnumerable().Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows);
        return await list.ToList();