2012-05-13 21 views
0

次のシナリオがあります。Doctorは複数のCompanionsを持つことができます。 Companionは、複数のDoctorsを持つこともできます。私は彼と一緒に旅してきた仲間のリストを特異博士を取得しようとしているエンティティフレームワーク:多対多

Public Class Doctor 

    Public Property DoctorId As Integer 
    Public Property Regeration As Integer 
    Public Property Name As String 
    Public Property Actor As String 
    Public Property Companions As List(Of Companion) 

End Class 

Public Class Companion 

    Public Property CompanionId As Integer 
    Public Property Name As String 
    Public Property Actor As String 
    Public Property Doctors As List(Of Doctor) 

End Class 

Public Class DoctorViewModel 

    Public Property DoctorId As Integer 
    Public Property Actor As String 
    Public Property Companions As List(Of CompanionViewModel) 

End Class 

Public Class CompanionViewModel 

    Public Property Actor As String 

End Class 

:ここに私のclassses(マイナスコンテキスト)があります。これは私が非常に簡単に行うことができますが、私はエンティティ全体ではなく、ほんの数個の列を取得するようにクエリを形作ることを試みています。彼は私の真面目な質問です - Xは私が理解できないものです。

Dim query = From d In context.Doctors 
       From c In context.Companions 
       Select New DoctorViewModel With { 
        .Actor = d.Actor, 
        .DoctorId = d.DoctorId, 
        .Companions = XXXXXXX} 

EDIT:私はクエリを実行した場合:

(From d In Tardis.Doctors 
Where d.Actor = "Tom Baker" 
Select New DoctorViewModel With {.Actor = d.Actor, .Companions = d.Companions.Select(Function(c) New CompanionViewModel With {.Actor = c.Actor})}).SingleOrDefault 

私が取得:

「タイプをキャストすることができません 'System.Collections.Generic.IEnumerable 1' to type 'System.Collections.Generic.List 1' LINQ。エンティティにのみ は、エンティティデータモデルプリミティブタイプのキャストをサポートします。

することは、(私のモデルが必要とされている機能の強打を持っている)、その後のViewModelにconstuctorにこれを渡し、クエリ内のViewModelクラスを捨て、ちょうど匿名型としてのものを得るために厄介であると考えられますこのようなクラスを記入しますか?

答えて

0

解決済み!私はこの日を過ごしました!トリックは、医者のViewModel内のリストをIEnumerable(Of CompanionViewModel)に変更することでした。

1

これはおそらく大丈夫ではなく、はるかに読みやすい(別名整理可能な)ものでもあります。特にクエリが匿名型を返さないため、IQueryableを返します。

+0

あなたの提案に感謝し、ちょうどそれを自分で解決しました。 –

関連する問題