【发布时间】:2015-07-03 09:06:55
【问题描述】:
以下是当前代码:
Parallel.ForEach(dataTable.AsEnumerable(),row => {
// Code to process the data row to Dictionary<object,object>
// Unique Column name is the Dictionary Key
// ConcurrentDictionary is used for thread safety
});
这里我使用Parallel.ForEach将DataTable的行处理成Dictionary<object,object>类型的对象,最终结果是List<Dictionary<object,object>>类型,使用中间线程安全结构ConcurrentQueue<Dictionary<object,object>>实现的,@的来源987654327@ 以给定的顺序对数据进行排序,但在并行处理期间总是会丢失。由于顺序很重要,所以我想出了以下解决方法:
Parallel.For(0,RowCount,index => {
int rowIndex = index;
// Access the rows using the Index
// Final structure will be of type ConcurrentDictionary<Custom>,
// with the RowIndex assigned based on original index
});
Class Custom
{
public int RowIndex { get; set; }
public Dictionary<object,object> DataDictionary {get; set;}
}
ConcurrentQueue<Dictionary<Custom>> customObj 类型的最终结果使用以下代码进行处理:
customObj.OrderBy(x=>x.RowIndex).Select(y=>y.DataDictionary).ToList()
以下是我的问题:
有没有更好的方法来实现相同的并行处理,我可以保持原始顺序,这是最重要的业务需求
在最终解决方案中我是否需要局部变量
rowIndex,我的理解是index是并行循环的一部分,不会导致关闭问题
任何指针?
【问题讨论】:
-
为什么要保留订单?你不能订购结果吗?
-
@Panagiotis Kanavos 订单是从数据源中保留的,基于可能不会暴露给数据库上方层的列。我们正在构建的分析应用程序的订单保留是一项重要的功能要求
标签: c# parallel-processing task-parallel-library parallel.foreach