我不得不使用客戶ID來從數據庫中獲取客戶的簡單方法:創建與LINQ和EF對象沒有定義其字段
Public Function GetCustomer(ByVal id As Long) As Customer
Using ctx As New MyEntities
Dim e = (From c In ctx.customers
Join bp In ctx.billingpoints
On c.customerId Equals bp.customerId
Select New Customer With {
.customerId = c.customerId
.billingPoint = New BillingPoint With{
.customerId = bp.customerId
.billingPointId = bp.billingPointId
'tons of more fields
}
'tons of more fields
})
Return e
End Using
End Function
這個方法返回一個這樣定義一個Customer對象:
Public Class Customer
Public Property customerId As Long
Public Property billingPoint As BillingPoint
'many more fields
End Class
正如你可以看到這看起來很可怕,所有的屬性被設置。有沒有更有效的方法來做到這一點?
你爲什麼不直接返回'C'? EF將自動填充所有屬性(包括'billingPoint')。 – glautrou