LINQ:InvalidOperationException:结果中不存在必需的列

时间:2014-05-23 17:51:34

标签: c# sql linq invalidoperationexception

我正在使用LINQ调用SQL Server sproc。偶尔我会得到InvalidOperationException:结果中不存在所需的列“ID”。该列确实存在于sproc结果中。我正在使用'使用'之类的建议。任何想法为什么会随机发生?

代码段:

using (MyDataContext dataContext = new MyDataContext(context.ConnectionString))
{
    alertQueueList = dataContext.ExecuteQuery<AlertQueue>("exec dbo.my_AlertQueue_Dequeue @RowsToReturn = {0}", "10");

    // To handle exception when no rows are returned by sproc
    ret = alertQueueList.ToList();
}
在ExecuteQuery()上抛出了

异常。

Exception:
System.InvalidOperationException: The required column 'ID' does not exist in the results.
   at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReaderBase`1.GetColumnOrdinals(NamedColumn[] namedColumns)
   at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReaderBase`1..ctor(ObjectReaderSession`1 session, NamedColumn[] namedColumns, Object[] globals, Object[] arguments, Int32 nLocals)
   at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReaderSession`1.CreateReader[TObject](Func`2 fnMaterialize, NamedColumn[] namedColumns, Object[] globals, Int32 nLocals, Boolean disposeDataReader)
   at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReaderFactory`2.Create(DbDataReader dataReader, Boolean disposeDataReader, IReaderProvider provider, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataContext.ExecuteMethodCall(Object instance, MethodInfo methodInfo, Object[] parameters)
   at System.Data.Linq.DataContext.ExecuteQuery[TResult](String query, Object[] parameters)
   at MyNamespace.Logic.AlertQueueLogic.GetForProcessing(IMyNamespaceContext context) in c:\\foo\\AlertQueueLogic.cs:line 26
   at Connect.Processor.AlertProcessor.DequeueAlerts() in c:\\foo\\AlertProcessor.cs:line 123

sproc内容: 我在下面的sproc中粘贴代码:

BEGIN TRANSACTION;

UPDATE TOP (@RowsToReturn) dbo.AlertQueue WITH (ROWLOCK, UPDLOCK, READPAST)
SET AlertStatus = @AlertStatus_Processing,
    LastLockedDttm = @CurrentDttm
    OUTPUT INSERTED.ID INTO @OutputRows
WHERE <my predicate>
COMMIT TRANSACTION;

SELECT  [ID], [ScheduleID], [TriggerName], [AlertStatus], [LastLockedDttm], [RetryCount], [LastRetryDttm], [LastResponse], [DateCreated], [Version]
FROM dbo.AlertQueue
WHERE ID IN (SELECT ID FROM @OutputRows)

3 个答案:

答案 0 :(得分:1)

在TransactionScope()下的C#代码中调用了sproc,它为与sproc中的READPAST提示冲突的事务添加了可序列化的隔离级别。删除READPAST或使TransactionScope()成为READCOMITTED隔离级别修复了问题。

答案 1 :(得分:0)

确保您有一个名为&#34; ID&#34;的列。它应该是主键。试试吧。

答案 2 :(得分:0)

也许这取决于你总是初始化为10的参数@RowsToReturn。也许在某些情况下,sproc查询的某些表中的行少于10行。