在PLINQ中正确使用ForAll

时间:2014-06-02 06:02:58

标签: vb.net entity-framework plinq

我有一个代码,它迭代LINQ检索的列表,并创建了一个带有一些附加属性的新对象列表。我想通过使用PLINQ和ForAll()来提高性能。 我尝试了以下内容;

Dim RetrieveList_Qry = From Prod In DB.Products.AsParallel().AsOrdered()
                                   Where Prod.ProductParentID > 0

If Me.AuthorityList Is Nothing Then
  Dim AuthorityGroup_Qry = From AG In DB.AuthorityGroups
  AuthorityList = AuthorityGroup_Qry.ToList
End If

Dim product As Product
ProdList = New ObjectModel.ObservableCollection(Of Product)

RetrieveList_Qry.ForAll(Sub(item)

product = New Product(item.ProductID,
                      item.ProductType,
                      item.ProductLabel )

*For Each PA As In item.ProductAuthorities*
   Dim AG As Data.AuthorityGroup = (From AuthorityGroup In Me. AuthorityList Where   
   PA.AuthorityGroupID = AuthorityGroup.AuthorityGroupID).FirstOrDefault
   product.AddProductAuthority(PA.ProductID,
   PA.AuthorityGroupID,
   PA.IsActive,
   AG.AuthorityGroupName)
Next
SyncLock (ProdList)
 ProdList.Add(product)
End SyncLock

End Sub)

但这似乎不起作用。有时它会给出结果,但有时会在上面代码的斜体线上给出以下异常: "无法在实体类型System.Data.Entity.DynamicProxies.AuthorityGroup_79066B4E83621583B6152BDC上设置字段/属性ProductAuthorities。"

我做错了什么?

0 个答案:

没有答案