2010-02-16 50 views
0

我已經生成實體框架4下面的代碼導航特性:轉換IEnumerable的到EntityCollection實體框架4

<XmlIgnoreAttribute()> 
<SoapIgnoreAttribute()> 
<DataMemberAttribute()> 
<EdmRelationshipNavigationPropertyAttribute("Diagnose", "Request_Comments", "Comment")> 
Public Property Comments() As EntityCollection(Of Comment) 
    Get 
     Return CType(Me,IEntityWithRelationships).RelationshipManager.GetRelatedCollection(Of Comment)("Diagnose.Request_Comments", "Comment") 
    End Get 
    Set 
     If (Not value Is Nothing) 
      CType(Me, IEntityWithRelationships).RelationshipManager.InitializeRelatedCollection(Of Comment)("Diagnose.Request_Comments", "Comment", value) 
     End If 
    End Set 
End Property 

當我想通過添加一個WHERE子句將其減少從得到的結果這樣

Return CType(Me,IEntityWithRelationships).RelationshipManager.GetRelatedCollection(Of Comment)("Diagnose.Request_Comments", "Comment") _ 
.Where(Function(p) p.IsDeleted = False) 

或使用LINQ

Return From x In CType(Me,IEntityWithRelationships).RelationshipManager.GetRelatedCollection(Of Comment)("Diagnose.Request_Comments", "Comment") _ 
Where x.IsDeleted = False Select x 

以下異常

Unable to cast object of type 'WhereEnumerableIterator`1[DAL.Comment]' to type 'System.Data.Objects.DataClasses.EntityCollection`1[DAL.Comment]'. 

我不知道如何轉換結果(這是一個IEnumerable我認爲)從查詢到EntityCollection:當我運行應用程序時拋出。我試圖添加一個「.ToList」,但沒有幫助。
有沒有人知道解決這個問題或更好的方法來從集合中刪除已刪除的項目?

回答

0

你真的需要它成爲一個EntityCollection嗎? 您可以創建一個新的只讀屬性,如下所示:

Public ReadOnly Property ActiveComments() As IEnumerable(Of Comment) 
    Get 
     Return From x In CType(Me,IEntityWithRelationships).RelationshipManager.GetRelatedCollection(Of Comment)("Diagnose.Request_Comments", "Comment") _ 
Where x.IsDeleted = False Select x 
    End Get 
End Property