Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

.net - Using Linq to populate a BindingSource with cross table joins

Forgive me if my terminology is wrong. I have the following DataSet:

enter image description here

I have a master-detail page via drag and dropping from VS DataSources window which lets a user choose a specific build for which it shows the build details and also a further detail page of every SupplierComponent used in each build:

enter image description here

What I am wanting is for the DataGridView on the top right (BuildSupplierComponents by BuildID) to show details from the linked Supplier and Component tables (via the SupplierComponent table) rather than the ID of the entry in the SupplierComponent table.

I thought I could use Linq to perform the joins (essentially to a depth of 2?) and then set the Datasource of the top right DataGridView to the linq results. Thus I tried the following code:

Private Sub BuildBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles BuildBindingSource.CurrentChanged
    If BuildBindingSource.Current Is Nothing Then Return

    Dim BuildID As Integer = DirectCast(BuildBindingSource.Current.row, MainDataSet.BuildRow).ID

    Dim var = From bsc In DirectCast(MainDS.Tables("BuildSupplierComponents"), MainDataSet.BuildSupplierComponentsDataTable)
              Join sc In DirectCast(MainDS.Tables("SupplierComponent"), MainDataSet.SupplierComponentDataTable) On sc.ID Equals bsc.SupplierComponentID
              Join s In DirectCast(MainDS.Tables("Supplier"), MainDataSet.SupplierDataTable) On s.ID Equals sc.SupplierID
              Join c In DirectCast(MainDS.Tables("Component"), MainDataSet.ComponentDataTable) On c.ID Equals sc.ComponentID
              Where bsc.BuildID = BuildID
              Select New With {.Supplier = s.Name, .component = c.Name}

    BuildSupplierComponentsBindingSource.DataSource = var

End Sub

However, this produces a null result, even when there is data in the BuildSupplierComponents.

Any ideas how I can achieve this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...