Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq和foreach中的并行处理_C#_Multithreading_Plinq_Parallel.foreach - Fatal编程技术网

C# Linq和foreach中的并行处理

C# Linq和foreach中的并行处理,c#,multithreading,plinq,parallel.foreach,C#,Multithreading,Plinq,Parallel.foreach,这是我的代码: int j; WebShopEntities data = new WebShopEntities(); var db = data; var list = (from line in System.IO.File.ReadLines(MyHttpApplication.GetAppDataPath() + "677254_dp_articles.TXT").AsParallel()

这是我的代码:

        int j;
        WebShopEntities data = new WebShopEntities();
        var db = data;
        var list =
        (from line in System.IO.File.ReadLines(MyHttpApplication.GetAppDataPath() + "677254_dp_articles.TXT").AsParallel()
         where line.EndsWith(";") && !((line.StartsWith("prom_erp_partno")))
         let parts = line.Split('\t')
         select new WebShop.dp_articles
         {
             prom_erp_partno = parts[0],
             prom_mfm_partno = parts[1],
             prol_name = parts[2],
             mfm_short_name = parts[3],
             prom_prfm_id=int.TryParse(parts[4],out j)?int.Parse(parts[4]):0,
             prol_lng_id=int.Parse(parts[5]),
             prol_variant=parts[6]??null,
             vpl=parts[7]??null,
             status=parts[8],
             gross_weight=Decimal.Parse('0'+parts[9].Replace('.',',')),
             commodity_code=parts[10],
             returnable = parts[11].Replace(';', ' ').Trim()

         }).ToList();
        Parallel.ForEach(list, item =>
         {

             if (!(from x in db.dp_articles.AsParallel() where x.prom_erp_partno == item.prom_erp_partno select x).Any())
             {

                 db.dp_articles.Add(new dp_articles
                 {
                     prom_erp_partno = item.prom_erp_partno,
                     prom_mfm_partno = item.prom_mfm_partno,
                     prol_name = item.prol_name,
                     mfm_short_name = item.mfm_short_name,
                     prom_prfm_id = item.prom_prfm_id,
                     prol_lng_id = item.prol_lng_id,
                     prol_variant = item.prol_variant,
                     vpl = item.vpl,
                     status = item.status,
                     gross_weight = item.gross_weight,
                     commodity_code = item.commodity_code,
                     returnable = item.returnable

                 });
             }
             else
             {

                 var itemU = db.dp_articles.Find(item.prom_erp_partno);
                 itemU.prom_mfm_partno = item.prom_mfm_partno;
                 itemU.prol_name = item.prol_name;
                 itemU.mfm_short_name = item.mfm_short_name;
                 itemU.prom_prfm_id = item.prom_prfm_id;
                 itemU.prol_lng_id = item.prol_lng_id;
                 itemU.prol_variant = item.prol_variant;
                 itemU.vpl = item.vpl;
                 itemU.status = item.status;
                 itemU.gross_weight = item.gross_weight;
                 itemU.commodity_code = item.commodity_code;
                 itemU.returnable = item.returnable;
             }

             db.SaveChanges();
         });
我需要并行执行这些进程,但我没有关于c#中并行进程的任何信息。 在Google中快速搜索后,我发现了这些并行语法,但运行后,出现了以下错误:

创建模型时无法使用上下文。如果上下文在OnModelCreating方法内使用,或者如果多个线程同时访问同一上下文实例,则可能引发此异常。注意,DbContext和相关类的实例成员不能保证是线程安全的

和堆栈跟踪:

[InvalidOperationException:在创建模型时无法使用上下文。如果在OnModelCreating方法中使用上下文,或者如果多个线程同时访问同一上下文实例,则可能引发此异常。请注意,DbContext和相关类的实例成员不保证是线程安全的.] System.Data.Entity.Internal.LazyInternalContext.InitializeContext()+797 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型entityType)+18 System.Data.Entity.Internal.Linq.InternalSet
1.Initialize()+53
System.Data.Entity.Internal.Linq.InternalSet
1.GetEnumerator()+15 System.Data.Entity.Infrastructure.DbQuery
1.System.Collections.Generic.IEnumerable.GetEnumerator()+53
System.Linq.Parallel.PartitionedDataSource
1.InitializePartitions(IEnumerable
1 source,Int32 partitionCount,Boolean useStriping)+501
System.Linq.Parallel.PartitionedDataSource
1..ctor(IEnumerable
1 source,Int32 partitionCount,Boolean useStriping)+92
System.Linq.Parallel.ExchangeUtilities.PartitionDataSource(IEnumerable
1源代码,Int32分区计数,布尔useStriping)+277 System.Linq.Parallel.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient
1 recipient)+92
System.Linq.Parallel.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient
1收件人)+578 System.Linq.Parallel.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient
1 recipient)+441
System.Linq.Parallel.QueryOperator
1.GetOpenedUnomerator(可为null的
1合并选项、布尔抑制顺序、布尔前效果、查询设置查询设置)+386
System.Linq.Parallel.queryOpenEnumerator
1.OpenQuery()+218 System.Linq.Parallel.QueryOpeningEnumerator
1.MoveNext()+32
System.Linq.Parallel.AnyAllSearchOperator
1.Aggregate()+64 System.Linq.ParallelEnumerable.Any(ParallelQuery
1源代码,Func
2谓词)+92 System.Linq.ParallelEnumerable.Any(ParallelQuery
1源代码)+126
WebShop.Models.c__在c:\Users\Ahmad\Documents\visualstudio 2013\Projects\WebShop\WebShop\Models\Ftp.cs:369中显示Class4f.b__;4b(dp_文章项)
System.Threading.Tasks.c_uuuDisplayClass2D
2.b_uuu23(Int32 i)+63 System.Threading.Tasks.c_uuuDisplayClassF
1.b_uuuC()+910
System.Threading.Tasks.Task.InnerInvoke()+72
System.Threading.Tasks.Task.InnerInvokeWitchArg(任务子任务)+17
System.Threading.Tasks.c__DisplayClass11.b__10(对象参数0)+198
[AggregateException:发生了一个或多个错误。]
System.Threading.Tasks.Task.ThrowifeException(布尔includeTaskCanceledException)+3834425
System.Threading.Tasks.Task.Wait(Int32毫秒计时,CancellationToken CancellationToken)+10919077
System.Threading.Tasks.Task.Wait()+10
System.Threading.Tasks.Parallel.ForWorker(Int32 fromInclusive,Int32 toExclusive,ParallelOptions ParallelOptions,Action
1正文,Action
2正文WithState,Func
4正文WithLocal,Func
1 LocalUnit,Action
1 localFinally)+925 System.Threading.Tasks.Parallel.ForEachWorker(IList
1列表,ParallelOptions并行选项,Action
1正文,Action
2正文带状态,Action
3正文带状态和索引,Func
4正文带状态和局部,Func
5正文带一切,Func
1局部,Action
1局部最终)+223 System.Threading.Tasks.Parallel.ForEachWorker(IEnumerable
1源,ParallelOptions并行选项,Action
1正文,Action
2正文带状态,Action
3正文带状态和索引,Func
4正文带状态和局部,Func
5正文带一切,Func
1局部,Action
1局部最终)+10893169 System.Threading.Tasks.Parallel.ForEach(IEnumerable
1源代码,Action
1正文)+110 c:\Users\Ahmad\Documents\Visual Studio 2013\Projects\WebShop\WebShop\Models\Ftp.ArticlesParser()中的WebShop.Models.Ftp.ArticlesParser() c:\Users\Ahmad\Documents\Visual Studio 2013\Projects\webshop\webshop\Controllers\HomeController.Index()中的webshop.Controllers.HomeController.Index() lambda_方法(闭包、控制器基、对象[])+62 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)+14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary
2参数)+156
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary
2个参数)+27 System.Web.Mvc.Async.AsyncControllerActionInvoker.b_u36(IAsyncResult asyncResult,ActionInvoking innerInvokeState)+22 System.Web.Mvc.Async.WrappedAsyncResult
2.CallEndDelegate(IAsyncResult asyncResult)+29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3c()+50 System.Web.Mvc.Async.c_udisplayClass45.b_u3e()+225 System.Web.Mvc.Async.c__显示类30.b__2f(IAsyncResult asyncResult)+10 System.Web.Mvc.Async.WrappedAsyncRes
if (!(from x in db.dp_articles.AsParallel() where x.prom_erp_partno == item.prom_erp_partno select x).Any())
var list =
        (from line in System.IO.File.ReadLines(MyHttpApplication.GetAppDataPath() + "677254_dp_articles.TXT").AsParallel()