我正在使用
感谢您,/David
Parallel.ForEach
循环遍历LINQ to SQL映射。一旦处理了一定数量的元素,我就跳出循环。处理停止,但是循环会在几秒钟后挂起,然后我会收到以下错误信息:System.Data.SqlClient.SqlErrorCollection: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
如何使循环正常退出?追加查询时强制执行本地集合(例如ToList()
)不可行。我还尝试将所有内容包装在using
块中,但无济于事。请注意,
以下是代码:var query = SomeDataContext.SomeTableMapping; // All ok, if I append Take(maxRecords)
int maxRecords = 1000;
Parallel.ForEach(query, (queryResult, pLoopState, idx) =>
{
// Do whatever here on queryResult.
if (idx > maxRecords)
{
Console.WriteLine("Reached maximum number of records: {0}", maxRecords);
pLoopState.Break();
}
});
感谢您,/David
pLoopState.ShouldExitCurrentIteration
没有帮助,错误仍然存在。请注意,我已编辑原帖。即使错误相同,我实际上是遍历映射本身 :-/ - OG Dude