I am using Entity Framework 4.1 code first+MVC3 and the inheritence stratagy that I use is TPC
I have the following classes
Public Class ObjectBase
<Key()>
Public Property Id As Integer
Public Property Description As String
End Class
Public Class Computer
Inherits ObjectBase
Public Property Computername As String
End Class
Public Class Book
Inherits ObjectBase
Public Property BookName As String
End Class
Public Class User
<Key()>
Public Property Id As Integer
Public Property Name As String
End Class
Public Class BorrowObject
<Key()>
Public Property Id As Integer
Public Property User As User
Public Property BorrowedObject as ObjectBase
End Class
Public Class BorrowComputerVM
<Key()>
Public Property Id As Integer
Public Property User As User
Public Property Computer as Computer
End Class
My questions are:
- How do I do a query (using LINQ,
Entity SQL or other commonly used
way) to get all BorrowObjects where
BorrowedObject is of type Computer? - How do I map the result of the query
to the ViewModel called
“BorrowComputerVM” (used for
creating views only used for
borrowing a Computer).
Question 1 (and question 2) should be very simple, but I have allready spent hours on Google to find an answer with no result at all. The only thing I have found is that you can get all computers in ObjectBase by writing context.ObjectBase.OfType(Of Computer), and that does not help since you cannot write context.BorrowObjects.ObjectBase.OfType(Of Computer)
Please provide code samples in VB.NET (if you can), but more importantly: Please ensure that the codesamples you supply work without hours of modification!
I write it in C#:
You can omit the
Includeif you don’t want to eager load theComputer.Includesare not necessary if you project into a new type:UserandComputerwill be loaded in theBorrowComputerVMalso without explicitInclude.The
isandasoperators work indeed in LINQ to Entities.