gpt4 book ai didi

c# - ToList()、Any()、Count()、Sum() 上的 Linq 错误

转载 作者:行者123 更新时间:2023-11-30 18:12:39 25 4
gpt4 key购买 nike

我收到以下错误,它们都与 system.linq 相关。奇怪的是,该方法有时似乎有效,但错误并没有持续发生。

Count() 代码:

using (var dbContext = new Entities(GlobalStuff.EntityConnection))
{
heartBeat.NumSlipsInSecurityUploadQueue = dbContext.SecurityUploadQueues.Count();
heartBeat.NumSlipsInDataStreamQueue = dbContext.DataStreamQueues.Count();
}

SecurityUploadQueues 属性:

Public ReadOnly Property SecurityUploadQueues() As ObjectSet(Of SecurityUploadQueue)
Get
If (_SecurityUploadQueues Is Nothing) Then
_SecurityUploadQueues = MyBase.CreateObjectSet(Of SecurityUploadQueue)("SecurityUploadQueues")
End If
Return _SecurityUploadQueues
End Get
End Property

Private _SecurityUploadQueues As ObjectSet(Of SecurityUploadQueue)

SecurityUploadQueue 属性是一个 EntityObject

计数()错误:

Error Uploading HeartBeat System.InvalidOperationException: Sequence contains more than one element
at System.Linq.Enumerable.Single[TSource](IEnumerable1 source)
at System.Data.Objects.ELinq.ObjectQueryProvider.<>c__11
1.b__11_3(IEnumerable1 sequence)
at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable
1 query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at SettlerService.SettlerService.UploadHeartBeat()

任何()代码:

var dataToUpload = (from bet in dbContext.DataStreamQueues select bet).Take(200);
if (dataToUpload.Any())

任何()错误:

System.InvalidOperationException: The specified cast from a materialized 'System.Int32' type to the 'System.Boolean' type is not valid. at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader1.GetValue(DbDataReader
reader, Int32 ordinal) at lambda_method(Closure , Shaper ) at
System.Data.Common.Internal.Materialization.Coordinator
1.ReadNextElement(Shaper shaper) at System.Data.Common.Internal.Materialization.Shaper1.SimpleEnumerator.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable
1 source) at System.Data.Objects.ELinq.ObjectQueryProvider.<>c__111.<GetElementFunction>b__11_3(IEnumerable1 sequence) at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable1
query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.Any[TSource](IQueryable
1 source)
at SettlerService.SettlerService.UploadDataStream()

我已经进行了一些谷歌搜索,但在这个问题上找不到太多信息。我认为这可能与 system.core 库(system.linq 是其中的一部分)的版本有关,但看不到问题。我正在使用 .Net 4.7.2

最佳答案

第二个 错误意味着您的 DataStreamQueues 数据库表中有一列的类型为 Int32 但等效属性的类型在DataStreamQueues 实体是 bool;所以检查所有列和属性类型是否匹配。
关于第一个异常,有点奇怪!因为此异常表明您在代码中的某处对具有多个项目的集合调用 Single() 方法,并且 Single() 只需要一个项目,它会引发异常,但我不会在提供的代码中查看对 Single() 的任何调用。

关于c# - ToList()、Any()、Count()、Sum() 上的 Linq 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55020913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com