我有这个查询:
// _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:不存在数据时尝试读取无效
使用以下堆栈跟踪:
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()
这没有任何意义。我该如何解决这个问题?