Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. LINQ Syntax, Join 2 Tables - Output new table using .CopyToDataTable

LINQ Syntax, Join 2 Tables - Output new table using .CopyToDataTable

Scheduled Pinned Locked Moved LINQ
helpcsharpdatabaseoraclelinq
1 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Elwad
    wrote on last edited by
    #1

    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>)'

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups