Конвертировать IEnumerable в EntityCollection Entity Framework 4 - PullRequest
0 голосов
/ 16 февраля 2010

Я создал следующий код с 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

Когда я хочу уменьшить результаты из GET, добавив к нему 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», но это не помогло.
Кто-нибудь знает решение этой проблемы или лучший способ выбросить удаленные элементы из коллекции?

1 Ответ

0 голосов
/ 24 февраля 2010

Вам действительно нужно, чтобы это было 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
...