码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-04-22 09:23:59      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:

具体的异常信息如下,一开始没有写日志只看到错误信息: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_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

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

标签:

原文地址:http://www.cnblogs.com/ithome8/p/5419962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!