Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8703893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:58:07+00:00 2026-06-13T02:58:07+00:00

I am trying to inner join 2 DataTables on 2 DataColumns using Linq, I

  • 0

I am trying to inner join 2 DataTables on 2 DataColumns using Linq, I can get the rows using Linq how ever I cannot get the columns. I have commented out what I tried to do to get the columns, shown below.

The reason for doing this.

I am planning on making 3 methods, LEFT JOIN, FULL JOIN and INNER JOIN that will return a DataTable, I am trying to make it as generic as possible, I am not show if the below is a good way or not, I need to do this to be as Optimized as possible.

     public static DataTable InnerJoin
     (DataTable dt1, DataTable dt2,DataColumn Parent,DataColumn Child)
     {
          DataTable result = new DataTable();


          var query =
              from d in dt1.AsEnumerable()
              join c in dt2.AsEnumerable() on d[Parent] equals c[Child]
              select new { d, c };
         /*
         var columns  = from d in dt1.Columns.Cast<DataColumn>()
                        from c in dt2.Columns.Cast<DataColumn>()
                          select new{c,d}
          foreach (var column in columns)
          {
              result.Columns.Add(column);
          }
          **/

          foreach (var row in query)
          {
              result.Rows.Add(row);
          }

         return result;
     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:58:08+00:00Added an answer on June 13, 2026 at 2:58 am

    Your code compiles since there’s an overload which takes a params object[]. But then you need to add two columns with type DataRow.

    public static DataTable InnerJoin(DataTable dt1, DataTable dt2, DataColumn Parent, DataColumn Child)
    {
        DataTable result = new DataTable();
        result.Columns.Add("Row1", typeof(DataRow));
        result.Columns.Add("Row2", typeof(DataRow));
    
        var query =
            from row1 in dt1.AsEnumerable()
            join row2 in dt2.AsEnumerable() on row1[Parent] equals row2[Child]
            select new { row1, row2 };
    
        foreach (var x in query)
        {
            result.Rows.Add(x.row1, x.row2);
        }
    
        return result;
    }
    

    If that’s what you need, it should work.

    Edit: and here’s a (working) test

    DataTable table1 = new DataTable("table1");
    table1.Columns.Add("ID", typeof(int));
    table1.Columns.Add("Name", typeof(String));
    
    object[] a1 = { 1, "T1Row1Name" };
    object[] a2 = { 2, "T1Row2Name" };
    object[] a3 = { 3, "T1Row3Name" };
    object[] a4 = { 4, "T1Row4Name" };
    table1.Rows.Add(a1);
    table1.Rows.Add(a2);
    table1.Rows.Add(a3);
    table1.Rows.Add(a4);
    
    DataTable table2 = new DataTable("table2");
    table2.Columns.Add("ID", typeof(int));
    table2.Columns.Add("Name", typeof(String));
    table2.Columns.Add("Table1ID", typeof(int));
    
    
    object[] b1 = { 1, "T2Row1Name", 1 };
    object[] b2 = { 2, "T2Row2Name", 2 };
    object[] b3 = { 3, "T2Row3Name", 2 };
    object[] b4 = { 4, "T2Row4Name", 2 };
    object[] b5 = { 5, "T2Row5Name", 3 };
    table2.Rows.Add(b1);
    table2.Rows.Add(b2);
    table2.Rows.Add(b3);
    table2.Rows.Add(b4);
    table2.Rows.Add(b5);
    
    var joinedTable = InnerJoin(table1, table2, table1.Columns["ID"], table2.Columns["Table1ID"]);
    
    foreach (DataRow r in joinedTable.Rows)
    {
        DataRow r1 = r.Field<DataRow>("Row1");
        DataRow r2 = r.Field<DataRow>("Row2");
        String r1Name = r1.Field<String>("Name");
        String r2Name = r2.Field<String>("Name");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an INNER JOIN query. I wanted to get rows/information that
I'm trying to find the inner text value of an element using LINQ-to-XML (an
Using extension syntax I'm trying to create a left-join using LINQ on two lists
I get a error when trying to inner join databases from a MySqL server.
I am trying to inner join 2 temp tables I know this can be
I'm trying to force Linq to preform an inner join between two tables. I'll
I am trying to use two inner join as foreign key. Actually, I have
In my Linq, I am trying to make an inner join to a nullable
Trying to get an INNER JOIN to work is driving me insane, so I'm
I am trying to use TWICE inner join statement to get reference values in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.