有一点背景,我是一名正在开发他的第一个Winform
的网页开发人员。
我正在使用EF5
。这些表是关系表,但表只有一个主键。我有一个Winform
附加GridView
。此GridView
由BindingSource
填充。 Linq查询正在填充Datasource
的{{1}}。
BindingSource
我的问题是Public Class Form1
Private batchEnt As BatchMananger.PrintManagerEntities
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
batchEnt = New BatchMananger.PrintManagerEntities
batchBindingSource.DataSource = (From b In batchEnt.AutomatedBatches Join bd In batchEnt.AutomatedBatchResults On b.ID Equals bd.AutomatedBatchID Where b.BatchName <> "" Select b.ID, b.BatchName, b.Description, b.ScheduleDesc, b.BatchResultEmail, b.BatchSourceEmail, bd.ExecutionDateTime, bd.TotalSuccesful, bd.TotalItems, bd.TotalFail).ToList
Catch ex As Exception
Throw
End Try
End Sub
End Class
只填充了Gridview
表中的数据,而不是来自联接表AutomatedBatches
的数据。但是,在检查AutomatedBatchResults
元素时,我发现联接的结果正在返回。我将如何绑定到Datasource
,以便BIndingsource
查询的所有结果都填充我的Linq
。
如果您需要更多信息,请与我们联系。
发现了什么问题。我没有在GridView的属性上设置DataPropertyName。将DataPropertyName设置为与其工作的数据库字段相同的名称。我还在EF数据模型中创建了AutomatedBatch(1)与AutomatedBatchResult(很多)之间的关联。
答案 0 :(得分:0)
如果没有看到您的数据,那么您的加入请求会导致联接表中的某些记录无法显示。请记住,linq中的join关键字是内连接,因此不会显示任何不符合连接条件的记录。要显示表格中的所有记录,请执行以下操作:
快速注意 - 请在所有对象上添加类型。
Private batchEnt As BatchMananger.PrintManagerEntities
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
Dim batchEnt As PrintManagerEntities = New PrintManagerEntities
Dim batchBindingSource
Dim batcheswithResults = (From b In batchEnt.AutomatedBatches
Join bd In batchEnt.AutomatedBatchResults On b.ID Equals bd.AutomatedBatchID
Where b.BatchName <> ""
Select New Results(b, bd)).ToList
Dim batchesWithoutResults = (From b In batchEnt.AutomatedBatches
Where Not batchEnt.AutomatedBatchResults.Any(Function(o) o.Id = b.Id)
Select New Results(b, Nothing)).ToList
Dim itemsSource As New List(Of Object)
itemsSource.AddRange(batcheswithResults)
itemsSource.AddRange(batchesWithoutResults)
batchBindingSource.DataSource = itemsSource
Catch ex As Exception
Throw
End Try
End Sub
End Module
Public Class Results
'TODO: Add types to params and all properties
Public Sub New(AutomaticBatches, AutomatedBatchResults)
_automaticBatches = AutomaticBatches
_automatedBatchResults = AutomatedBatchResults
End Sub
Private _automaticBatches
Private _automatedBatchResults
Public Property ID
Get
Return _automaticBatches.ID
End Get
Set(value)
_automaticBatches.ID = value
End Set
End Property
Public Property BatchName
Get
Return _automaticBatches.BatchName
End Get
Set(value)
_automaticBatches.BatchName = value
End Set
End Property
Public Property Description
Get
Return _automaticBatches.Description
End Get
Set(value)
_automaticBatches.Description = value
End Set
End Property
Public Property ScheduleDesc
Get
Return _automaticBatches.ScheduleDesc
End Get
Set(value)
_automaticBatches.ScheduleDesc = value
End Set
End Property
Public Property BatchResultEmail
Get
Return _automaticBatches.BatchResultEmail
End Get
Set(value)
_automaticBatches.BatchResultEmail = value
End Set
End Property
Public Property BatchSourceEmail
Get
Return _automaticBatches.BatchSourceEmail
End Get
Set(value)
_automaticBatches.BatchSourceEmail = value
End Set
End Property
Public Property ExecutionDateTime
Get
If _automatedBatchResults IsNot Nothing Then Return _automatedBatchResults.ExecutionDateTime
Return Nothing
End Get
Set(value)
If _automatedBatchResults IsNot Nothing Then _automatedBatchResults.ExecutionDateTime = value
End Set
End Property
Public Property TotalSuccesful
Get
If _automatedBatchResults IsNot Nothing Then Return _automatedBatchResults.TotalSuccesful
Return Nothing
End Get
Set(value)
If _automatedBatchResults IsNot Nothing Then _automatedBatchResults.TotalSuccesful = value
End Set
End Property
Public Property TotalItems
Get
If _automatedBatchResults IsNot Nothing Then Return _automatedBatchResults.TotalItems
Return Nothing
End Get
Set(value)
If _automatedBatchResults IsNot Nothing Then _automatedBatchResults.TotalItems = value
End Set
End Property
Public Property TotalFail
Get
If _automatedBatchResults IsNot Nothing Then Return _automatedBatchResults.TotalFail
Return Nothing
End Get
Set(value)
If _automatedBatchResults IsNot Nothing Then _automatedBatchResults.TotalFail = value
End Set
End Property
End Class
答案 1 :(得分:0)
发现了什么问题。我没有在GridView的属性上设置DataPropertyName。将DataPropertyName设置为与其工作的数据库字段相同的名称。我还在EF数据模型中创建了AutomatedBatch(1)与AutomatedBatchResult(很多)之间的关联。