views:

683

answers:

1

I have generated the following code with Entity Framework 4 for a navigation property:

<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

When I want to reduce the results from the GET by adding a WHERE-Clause to it like this

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

or by using Linq

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

the following Exception is thrown when I run the application :

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

I don't know how to convert the result (which is an IEnumerable i think) from the query to an EntityCollection. I tried to add a ".ToList" but that didn't help.
Does anybody know a solution to that problem or a better way to kick out the deleted items from the collection?

A: 

Do you really need it to be an EntityCollection? You could create a new read-only property, something like this:

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
willbt