Forum Discussion
You can use LINQ to join two datatables.
Hi Tom, I actually have a really small dataset from the application (i select the scheduled tasks from the task scheduler) and then loop over these results in a Datatable. For every line (i have only 😎 i run a query on the Framework with the item from the application and append that to a DataTable.
However, i cannot imagine that there is no way to join two datatables.
Curious what LINQ means that Celvin states. @Celvin, do you have some more information?
- Tom_R3 years agoNew Contributor III
Hi Celvin,
I have never used LINQ. Can you please provide an example of how it would work?
Thanks,
- ckattookaran3 years agoVIP
The code below joins two data tables using a single column.
ancDT table got the Name and Description of a member.
prcDT got the Process time information of the member (who processed the entity, when it was done, et al.)Now to present the user with the Description of the member along with the process information the following code is used.
dim dt as new DataTable() dt.Columns.Add("Code1") dt.Columns.Add("ProcessTime") dt.Columns.Add("ProcessBy") dt.Columns.Add("Process") dt.Columns.Add("WFScenarioName") dt.Columns.Add("WFTimeName") dt.Columns.Add("Description") Dim result ' perform a left outer join using both tables Dim query = From tl_ancDT In ancDT.AsEnumerable ' loop through table 1 Group Join tl_prcDT In prcDT.AsEnumerable On tl_ancDT.Field(Of String)("Name") Equals tl_prcDT.Field(Of String)("Code1") Into tl_ancDT_tl_prcDT = Group From tl_prcDT In tl_ancDT_tl_prcDT.DefaultIfEmpty() Select New With {Key .Code1 = tl_ancDT.Field(Of String)("Name"), Key .ProcessTime = If(tl_prcDT Is Nothing, DateTime.Parse("1/1/1900"), tl_prcDT.Field(Of DateTime)("ProcessTime")), Key .ProcessBy = If(tl_prcDT Is Nothing, "Admin", tl_prcDT.Field(Of String)("ProcessBy")), Key .Process = If(tl_prcDT Is Nothing, 0, tl_prcDT.Field(Of Boolean)("Process")), Key .WFScenarioName = If(tl_prcDT Is Nothing,String.Empty , tl_prcDT.Field(Of String)("WFScenarioName")), Key .WFTimeName = If(tl_prcDT Is Nothing,String.Empty , tl_prcDT.Field(Of String)("WFTimeName")), Key .Description = tl_ancDT.Field(Of String)("Description")} ' add to return table For Each result In query If Not String.IsNullOrEmpty(result.Code1) If result.ProcessTime.Year > 1900 dt.Rows.Add(result.Code1, result.ProcessTime, result.ProcessBy , 1, result.WFScenarioName, result.WFTimeName, result.Description) Else dt.Rows.Add(result.Code1, result.ProcessTime, result.ProcessBy , result.Process, result.WFScenarioName, result.WFTimeName, result.Description) End If End If Next