2010-02-16 3 views
0

J'ai généré le code suivant avec Entity Framework 4 pour une propriété de navigation:Convertir IEnumerable EntityCollection 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 

Quand je veux réduire les résultats de l'EEG en ajoutant une clause WHERE à elle comme celui-ci

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

ou en utilisant Linq

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

l'exception suivante est jeté quand je lance l'application:

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

Je ne sais pas comment convertir le résultat (qui est un IEnumerable je pense) de la requête à un EntityCollection. J'ai essayé d'ajouter un ".ToList" mais cela n'a pas aidé.
Est-ce que quelqu'un connaît une solution à ce problème ou une meilleure façon d'expulser les éléments supprimés de la collection?

Répondre

0

Avez-vous vraiment besoin que ce soit un EntityCollection? Vous pouvez créer une nouvelle propriété en lecture seule, quelque chose comme ceci:

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