LINQ Syntax, Join 2 Tables - Output new table using .CopyToDataTable
-
Hi, I have a very basic LINQ help request. I am attempting to join 2 tables using LINQ and then create a new table from the join. The equivelent SQL (in Oracle) would be: Create TableJoined as (Select t1.Name,t1.ID,t2.Result From table1 t1 join table2 t2 on t1.ID=t2.ID) With a table output of ID Name Result 1 Fauncy Yes 2 Elwad No However, I am doing something wrong in my LINQ syntax. The error message that I get is shown following the code. Can anyone suggest the correct syntax for what I want to do? Thanks, Wiley Osborn Dim oDatarow As DataRow 'Create & populate first table Dim table1 As New DataTable() table1.TableName = "Table1" table1.Columns.Add("Name", GetType(String)) table1.Columns.Add("ID", GetType(Integer)) oDatarow = table1.NewRow() oDatarow.Item("Name") = "Fauncy" oDatarow.Item("ID") = 1 table1.Rows.Add(oDatarow) oDatarow = table1.NewRow() oDatarow.Item("Name") = "Elwad" oDatarow.Item("ID") = 2 table1.Rows.Add(oDatarow) 'Create & populate second table Dim table2 As New DataTable() table2.TableName = "Table2" table2.Columns.Add("ID", GetType(Integer)) table2.Columns.Add("Result", GetType(String)) oDatarow = table2.NewRow() oDatarow.Item("Result") = "Yes" oDatarow.Item("ID") = 1 table2.Rows.Add(oDatarow) oDatarow = table2.NewRow() oDatarow.Item("Result") = "No" oDatarow.Item("ID") = 2 table2.Rows.Add(oDatarow) 'Use LINQ to join tables Dim oResult = _ From t1 In table1.AsEnumerable() _ Join t2 In table2.AsEnumerable() _ On t1.Field(Of Integer)("ID") Equals _ t2.Field(Of Integer)("ID") _ Select New With _ { _ .id = t1.Field(Of Integer)("ID"), _ .name = t1.Field(Of String)("Name"), _ .result = t2.Field(Of String)("Result") _ } 'Use CopyToDataTable to create a new table from the join '!!!THIS IS WHERE I GET THE DESIGN TIME ERROR SHOW BELOW!!! Dim oTableJoined As DataTable = oResult.CopyToDataTable() DESIGN ERROR is: 'CopytoDataTable' is not a member of 'System.Collections.Generic.IEnumerable(Of <anonymous type>)'