.net 将IEnumerable转换为EntityCollection实体框架4

.net 将IEnumerable转换为EntityCollection实体框架4,.net,entity-framework,collections,ienumerable,.net,Entity Framework,Collections,Ienumerable,我使用Entity Framework 4为导航属性生成了以下代码: <XmlIgnoreAttribute()> <SoapIgnoreAttribute()> <DataMemberAttribute()> <EdmRelationshipNavigationPropertyAttribute("Diagnose", "Request_Comments", "Comment")> Public Property Comments() As En

我使用Entity Framework 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
或者使用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”,但没有效果。

是否有人知道该问题的解决方案或从集合中删除已删除项目的更好方法?

您真的需要它成为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
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