c# ef报错_关于EF分页查询出错(Count must have a non-negative value.)的解决方案

关于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.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.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..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_kdt\branches\MEC2.2\src\Biz\ControlPanel\Depot\StoresHelper.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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值