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 4061310
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:27:05+00:00 2026-05-20T15:27:05+00:00

I have two Data Tables and these are completely dynamic. These would be generated

  • 0

I have two Data Tables and these are completely dynamic. These would be generated at runtime. Now I want to Join these tables by finding the common columns.

Kindly check below code for further information

public DataTable DataTableJoiner(DataTable dt1, DataTable dt2)
{
    using (DataTable targetTable = dt1.Clone())
    {
        var dt2Query = dt2.Columns.OfType<DataColumn>().Select(dc =>
            new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping));
        var dt2FilterQuery = from dc in dt2Query.AsEnumerable()
                             where targetTable.Columns.Contains(dc.ColumnName) == false
                             select dc;
        targetTable.Columns.AddRange(dt2FilterQuery.ToArray());
        var rowData=from row1 in dt1.AsEnumerable()
                    join row2 in dt2.AsEnumerable()
                    on row1.Field<int>("ID") equals row2.Field<int>("ID")
                    select row1.ItemArray.Concat(row2.ItemArray.Where(r2 => row1.ItemArray.Contains(r2) == false)).ToArray();
        foreach (object[] values in rowData) targetTable.Rows.Add(values); 
        return targetTable;
    }
}

In the above I have hardcoded “ID” as the common column. I need the common column to be produced/recognized dynamically. Please help me.

  • 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-05-20T15:27:05+00:00Added an answer on May 20, 2026 at 3:27 pm

    What about this, which worked for me:

    private DataTable DataTableJoiner(DataTable dt1, DataTable dt2)
        {
            var commonColumns = dt1.Columns.OfType<DataColumn>().Intersect(dt2.Columns.OfType<DataColumn>(), new DataColumnComparer());
    
            var result = new DataTable();
            result.Columns.AddRange(
                dt1.Columns.OfType<DataColumn>()
                .Union(dt2.Columns.OfType<DataColumn>(), new DataColumnComparer())
                .Select(c => new DataColumn(c.Caption, c.DataType, c.Expression, c.ColumnMapping))
                .ToArray());
    
            var rowData = dt1.AsEnumerable().Join(
                dt2.AsEnumerable(),
                row => commonColumns.Select(col => row[col.Caption]).ToArray(),
                row => commonColumns.Select(col => row[col.Caption]).ToArray(),
                (row1, row2) => 
                {
                    var row = result.NewRow();
                    row.ItemArray = result.Columns.OfType<DataColumn>().Select(col => row1.Table.Columns.Contains(col.Caption) ? row1[col.Caption] : row2[col.Caption]).ToArray();
                    return row;
                },
                new ObjectArrayComparer());
    
            foreach (var row in rowData)
                result.Rows.Add(row);
    
            return result;
        }
    

    For this to work, you need to declare these 2 classes in addition:

    private class DataColumnComparer : IEqualityComparer<DataColumn>
        {
    
            #region IEqualityComparer<DataColumn> Members
    
            public bool Equals(DataColumn x, DataColumn y)
            {
                return x.Caption == y.Caption;
            }
    
            public int GetHashCode(DataColumn obj)
            {
                return obj.Caption.GetHashCode();
            }
    
            #endregion
        }
    
        private class ObjectArrayComparer : IEqualityComparer<object[]>
        {
            #region IEqualityComparer<object[]> Members
    
            public bool Equals(object[] x, object[] y)
            {
                for (var i = 0; i < x.Length; i++)
                {
                    if (!object.Equals(x[i], y[i]))
                        return false;
                }
    
                return true;
            }
    
            public int GetHashCode(object[] obj)
            {
                return obj.Sum(item => item.GetHashCode());
            }
    
            #endregion
        }
    

    I hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have the equivalent of two completely unrelated tables in my database,
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have a two-dimensional array (of Strings) which make up my data table (of
I have two processes one will query other for data.There will be huge amount
I have two files (f1 and f2) containing some text (or binary data). How
I have two projects, the DLL project which has all my logic and data
I have a two dimensional array that I need to load data into. I
I have two processes: Writes to two tables every second (ish) Reads from said
Let's say I have two tables with several fields and in every table there
I am trying to compare two tables, SQL Server, to verify some data. I

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.