zoukankan      html  css  js  c++  java
  • 关于EF分页查询报错(Count must have a non-negative value.)的解决方案

    具体的异常信息如下,一开始没有写日志只看到错误信息:Count must have a non-negative value.,从表面意思可以看出来是Count值出现了负数,所以报错,查了半天的原因也没有看出来为什么,直到后面,我把Linq表达式输了出来,发现Skip(-10),我一想分页查询会根据页码跳过前面页码的记录数,所以会用Skip来跳过,但是这里的Skip数量为-10,这是为什么呢,我想了一下会不会是页码或者每页记录数有问题,于是我看了一下代码,果然发现没有给分页查询的PageIndex和PageSize赋值,所以PageIndex默认为0.PageSize默认为10.所以就会跳过(0-1)*10=-10条记录,导致报错,所以解决办法很简单了就是给PageIndex和PageSize赋值,果然修改之后一切正常。

    Expression:value(System.Data.Entity.Core.Objects.ObjectQuery`1[Hidistro.EFEntities.StoreCollectionInfo]).MergeAs(AppendOnly).Where(a => True).Where(x => (Convert(x.StoreId) == value(Hidistro.ControlPanel.Depot.StoresHelper+<>c__DisplayClass3c).query.StoreId)).Where(x => (x.StoreId == value(Hidistro.ControlPanel.Depot.StoresHelper+<>c__DisplayClass3c).query.StoreId.Value)).Where(item => True).OrderByDescending(item => item.PayTime).Skip(-10).Take(10)
    ErrorMessage:Count must have a non-negative value.
    参数名: count
    StackTrace: 在 System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder.DbExpressionBuilder.Skip(DbExpressionBinding input, IEnumerable`1 sortOrder, DbExpression count)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.OrderByLifter.OrderByLifterBase.ApplySortOrderToSkip(DbExpression input, DbSortExpression sort, DbExpression k)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.OrderByLifter.SortLifter.Skip(DbExpression k)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.OrderByLifter.Skip(DbExpressionBinding input, DbExpression skipCount)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Skip(DbExpressionBinding input, DbExpression skipCount)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SkipTranslator.TranslatePagingOperator(ExpressionConverter parent, DbExpression operand, DbExpression count)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.PagingTranslator.TranslateUnary(ExpressionConverter parent, DbExpression operand, MethodCallExpression call)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnarySequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnarySequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
    在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()
    在 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
    在 System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass3.<GetResults>b__2()
    在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
    在 System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass3.<GetResults>b__1()
    在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
    在 System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
    在 System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
    在 System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
    在 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
    在 Hidistro.ControlPanel.Depot.StoresHelper.GetStoreCollectionInfos(StoreCollectionsQuery query) 位置 g:Hishop_kdtranchesMEC2.2srcBizControlPanelDepotStoresHelper.cs:行号 1422
    BaseException:Count must have a non-negative value.
    参数名: count
    TargetSite:System.Data.Entity.Core.Common.CommandTrees.DbSkipExpression Skip(System.Data.Entity.Core.Common.CommandTrees.DbExpressionBinding, System.Collections.Generic.IEnumerable`1[System.Data.Entity.Core.Common.CommandTrees.DbSortClause], System.Data.Entity.Core.Common.CommandTrees.DbExpression)
    ExSource:EntityFramework

  • 相关阅读:
    201671010461张仲桃 实验三
    通读《构建之法》之后的问题
    201671010402-陈靖 实验十四 团队项目评审&课程学习总结
    201671010402-陈靖 实验四附加实验
    201671010402-陈靖——英文文本统计分析》结对项目报告
    201671010402 词频统计软件项目报告
    201671010402-陈靖 实验三 作业互评与改进
    读《现代软件工程——构建之法》所遇到的问题
    201671010403 陈倩倩 实验十四 团队项目评审&课程学习总结
    201671010403 陈倩倩 实验四附加实验
  • 原文地址:https://www.cnblogs.com/ithome8/p/5419962.html
Copyright © 2011-2022 走看看