gpt4 book ai didi

c# - "Invalid attempt to read when no data is present"在对查询结果启动 foreach 循环时

转载 作者:行者123 更新时间:2023-11-30 13:04:04 27 4
gpt4 key购买 nike

我有这个问题:

 // _db derives from DbContext
var toProcess = from jobItem in _db.Jobs
where jobItem.State == desiredState
select jobItem.ItemId;
foreach (Guid itemId in toProcess ) //exception thrown on this line
{
// whatever
}

大多数时候运行良好,但偶尔 foreach 会抛出:

System.InvalidOperationException: Invalid attempt to read when no data is present

具有以下堆栈跟踪:

System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i)
System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i)
System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
System.Data.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32 ordinal)
System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()

这没有任何意义。我该如何解决这个问题?

最佳答案

根据您对我评论的回复:

There's some medium weight activity that is expected to be done within several seconds but I guess it can take longer. Do you think a timeout happens?

我想这是一个超时或类似的东西。您可以增加超时时间,或者您可以强制它求值并将其存储在内存中供以后处理。您可以通过在查询末尾添加 ToArray() 来执行此操作:

 var toProcess = (from jobItem in _db.Jobs
where jobItem.State == desiredState
select jobItem.ItemId).ToArray();

看看是否有帮助。

关于c# - "Invalid attempt to read when no data is present"在对查询结果启动 foreach 循环时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10613744/

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