简体   繁体   English

Entity Framework Core错误的页面数据

[英]Wrong page data with Entity Framework Core

Recently Im trying to upgrade My MVC project to dotnetCore project, while something went wrong in BaseService.. 最近,我试图将My MVC项目升级到dotnetCore项目,而BaseService中出了点问题。

my former MVC code which works good: 我以前的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();
        }
}

and the dotnetCore code: 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;
}

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: 第1页:

第1页

page2: 第2页:

第2页

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM