Asp.net 使用 entity framework 到 运行 join 语句并在 GridView 中显示
Asp.net using entity framework to run join statement and display in GridView
Schedule
table:
ScheduleId(PK), AirplaneId(FK), OriginId(FK), Duration-String
Airplane
table:
AirplaneId(PK), PlaneCode-String
Origin
table:
OriginId(PK), OriginName-String
我想显示如下内容:
ScheduleId | PlaneCode | OriginName | Duration
Schedule.vb
Public Class Schedule
Public Property ID As Int32
Public Property Duration As String
'Foreign key of airplane_Id
Public Overridable Property Airplane As Airplane
'Foreign key of origin_Id
Public Overridable Property Origin As Origin
End Class
Airplane.vb
Public Class Airplane
Public Property ID As Int32
Public Property PlaneCode As String
End Class
Origin.vb
Public Class Origin
Public Property ID As Int32
Public Property OriginName As String
End Class
我的网格视图:
<asp:GridView ID="gvSearchFlight" runat="server" ItemType="CIM.Schedule" SelectMethod="gvSearchFlight_GetData" AutoGenerateColumns="false" GridLines="None">
GridView 的 SelectMethod
:
Dim dbContext As New ApplicationDbContext
Public Function gvSearchFlight_GetData() As IQueryable(Of Schedule)
Dim schedules = (From sche In dbContext.Schedule
Join air In dbContext.Airplane
On sche.Airplane.ID Equals air.ID).ToList()
Return schedules
End Function
但是我一直收到这个错误:
Unable to cast object of type 'System.Collections.Generic.List1[VB$AnonymousType_11
2[CIM.Schedule,CIM.Airplane]]' to type 'System.Linq.IQueryable`1[CIM.Schedule]'
编辑:我真的不知道如何写"Select New"语句..
Public Class ScheduleInformation
Public Property Schedule As Schedule
Public Property Airplane As Airplane
Public Property Origin As Origin
End Class
Public Function gvSearchFlight_GetData() As IQueryable(Of ScheduleInformation)
Dim schedules = (From sche In dbContext.Schedule
Join air In dbContext.Airplane On sche.Airplane.ID Equals air.ID
Join ori In dbContext.Origin On sche.Origin.ID Equals ori.ID
Select New ScheduleInformation())
Return schedules
End Function
这是我解决问题的方法。
ScheduleInformation.vb
Public Class ScheduleInformation
Public Property Schedule As Schedule
Public Property Airplane As Airplane
Public Property Origin As Origin
End Class
GridView 的 SelectMethod
'GridView's SelectMethod to get data
Public Function gvSearchFlight_GetData() As IQueryable(Of ScheduleInformation)
Dim schedules = (From sche In dbContext.Schedule
Join air In dbContext.Airplane On sche.Airplane.ID Equals air.ID
Join ori In dbContext.Origin On sche.Origin.ID Equals ori.ID
Select New ScheduleInformation With {.Schedule = sche, .Airplane = air, .Origin = ori})
Return schedules
End Function
然后,在我的标签中,我是这样写的:
<asp:GridView ID="gvSearchFlight" runat="server" ItemType="CIM.ScheduleInformation" SelectMethod="gvSearchFlight_GetData" AutoGenerateColumns="false">...
Schedule
table:
ScheduleId(PK), AirplaneId(FK), OriginId(FK), Duration-String
Airplane
table:
AirplaneId(PK), PlaneCode-String
Origin
table:
OriginId(PK), OriginName-String
我想显示如下内容:
ScheduleId | PlaneCode | OriginName | Duration
Schedule.vb
Public Class Schedule
Public Property ID As Int32
Public Property Duration As String
'Foreign key of airplane_Id
Public Overridable Property Airplane As Airplane
'Foreign key of origin_Id
Public Overridable Property Origin As Origin
End Class
Airplane.vb
Public Class Airplane
Public Property ID As Int32
Public Property PlaneCode As String
End Class
Origin.vb
Public Class Origin
Public Property ID As Int32
Public Property OriginName As String
End Class
我的网格视图:
<asp:GridView ID="gvSearchFlight" runat="server" ItemType="CIM.Schedule" SelectMethod="gvSearchFlight_GetData" AutoGenerateColumns="false" GridLines="None">
GridView 的 SelectMethod
:
Dim dbContext As New ApplicationDbContext
Public Function gvSearchFlight_GetData() As IQueryable(Of Schedule)
Dim schedules = (From sche In dbContext.Schedule
Join air In dbContext.Airplane
On sche.Airplane.ID Equals air.ID).ToList()
Return schedules
End Function
但是我一直收到这个错误:
Unable to cast object of type 'System.Collections.Generic.List
1[VB$AnonymousType_11
2[CIM.Schedule,CIM.Airplane]]' to type 'System.Linq.IQueryable`1[CIM.Schedule]'
编辑:我真的不知道如何写"Select New"语句..
Public Class ScheduleInformation
Public Property Schedule As Schedule
Public Property Airplane As Airplane
Public Property Origin As Origin
End Class
Public Function gvSearchFlight_GetData() As IQueryable(Of ScheduleInformation)
Dim schedules = (From sche In dbContext.Schedule
Join air In dbContext.Airplane On sche.Airplane.ID Equals air.ID
Join ori In dbContext.Origin On sche.Origin.ID Equals ori.ID
Select New ScheduleInformation())
Return schedules
End Function
这是我解决问题的方法。
ScheduleInformation.vb
Public Class ScheduleInformation
Public Property Schedule As Schedule
Public Property Airplane As Airplane
Public Property Origin As Origin
End Class
GridView 的 SelectMethod
'GridView's SelectMethod to get data
Public Function gvSearchFlight_GetData() As IQueryable(Of ScheduleInformation)
Dim schedules = (From sche In dbContext.Schedule
Join air In dbContext.Airplane On sche.Airplane.ID Equals air.ID
Join ori In dbContext.Origin On sche.Origin.ID Equals ori.ID
Select New ScheduleInformation With {.Schedule = sche, .Airplane = air, .Origin = ori})
Return schedules
End Function
然后,在我的标签中,我是这样写的:
<asp:GridView ID="gvSearchFlight" runat="server" ItemType="CIM.ScheduleInformation" SelectMethod="gvSearchFlight_GetData" AutoGenerateColumns="false">...