私は次のシナリオを持っています:ADoctor
は複数を持つことができますCompanions
。ACompanion
は複数を持つこともできます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
私は彼と一緒に旅行した仲間のリストで単一の医者を取得しようとしています。これは非常に簡単に実行できますが、エンティティ全体ではなく、少数の列のみを取得するようにクエリを形成しようとしています。彼は私の困惑した質問です-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}
編集:私が照会した場合:
(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.IEnumerable1'をキャストできません。LINQtoEntities
1' to type 'System.Collections.Generic.List
は、エンティティデータモデルプリミティブタイプのキャストのみをサポートしています。」
クエリでViewModelクラスを捨てて、匿名タイプとして取得し、これをViewModelのコンストラクターに渡して(私のモデルには必要な関数がたくさんあります)、このようにクラスを埋めるのは厄介だと思われますか? ?