2016-11-21 49 views
0

我試圖用Parallel.ForEach轉換ForEachC#Parallel.ForEach,不工作

ForEach,工作完好,並給出預期的結果。當我嘗試使用Parallel.ForEach時,我得到一個List變量的錯誤,說它不可用。

我在做什麼錯?我在代碼中添加了註釋來解釋。

public class CalssTest 
    { 

     List<AssetDetails> assets; 
     AssetDetails asset; 


     private void loadData() 
     { 
      //Loading Assetdetails here 
      //Skipped here to keep the code short. 
     } 

     private void cmdValidate_Click(object sender, RoutedEventArgs e) 
     { 
      OrderManager om = new OrderManager(); 

      using (ATXFunctions atx = new ATXFunctions()) 
      { 
       //*****THIS WORKS *********//. 
       foreach (AssetDetails ad in assets) 
       { 
        ad.OrderEntry.OrderType= "A"; 
        ad.OrderEntry.Portfolio = "John"; 
        ad.OrderEntry.Quantity = 1000;      
        ad.Compliance.Result = om.RunCompliance(atx.AtxEngine, ad.StockEntry, ad.OrderEntry);    
       } 

        //*****THIS ERRORS OUT *********//. 
       // Error :: 
       //The name 'assets' does not exist in the current context 

       Parallel.ForEach (assets (ad) => 
       { 
        ad.OrderEntry.OrderType = "A"; 
        ad.OrderEntry.Customer = "John"; 
        ad.OrderEntry.Quantity = 1000; 
        ad.Compliance.Result = om.ValidateOrder(ad.StockEntry, ad.OrderEntry); 
       }); 


      } 
     } 
    } 
+2

缺少逗號? – Jon

+0

是的。我是一個白癡。在這個浪費的時間。 :)感謝您的迴應。 – teddy2

回答

4

你語法不正確使用:`assets`後

Parallel.ForEach(assets , (ad) => 
    { 
        ad.OrderEntry.OrderType = "A"; 
        ad.OrderEntry.Customer = "John"; 
        ad.OrderEntry.Quantity = 1000; 
        ad.Compliance.Result = om.ValidateOrder(ad.StockEntry, ad.OrderEntry); 
       }); 
+0

天才。謝謝。幾乎爲此哭泣。 :) – teddy2