次のシナリオがあります。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クラスを捨て、ちょうど匿名型としてのものを得るために厄介であると考えられますこのようなクラスを記入しますか?
あなたの提案に感謝し、ちょうどそれを自分で解決しました。 –