0

私は次のシナリオを持っています: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'をキャストできません。LINQtoEntities1' to type 'System.Collections.Generic.Listは、エンティティデータモデルプリミティブタイプのキャストのみをサポートしています。」

クエリでViewModelクラスを捨てて、匿名タイプとして取得し、これをViewModelのコンストラクターに渡して(私のモデルには必要な関数がたくさんあります)、このようにクラスを埋めるのは厄介だと思われますか? ?

4

2 に答える 2

1

それはおそらく大丈夫であるだけでなく、はるかに読みやすい(別名保守可能)でしょう。特に、クエリは匿名タイプを返さないため、IQueryableを返します。

于 2012-05-13T05:49:14.750 に答える
0

解決しました!私はこれに何日も費やしました!トリックは、DoctorViewModel内のリストを次のように変更することでした。IEnumerable(Of CompanionViewModel)

于 2012-05-13T05:51:06.083 に答える