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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:46:56+00:00 2026-05-29T20:46:56+00:00

I am using this below: public static DataTable DataTableJoiner(DataTable dt1, DataTable dt2) { using

  • 0

I am using this below:

    public static 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>("Code") equals 
                             row2.Field<int>("Code")
                          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;
        }
    }

There is a problem with this line:

select row1.ItemArray.Concat(row2.ItemArray.Where(r2 => 
     row1.ItemArray.Contains(r2) == false)).ToArray();

It seems to be saying don’t include me if this value (rather than column) already exists.

I am using this method to join two tables together based on a column that both tables share, but I only want the unique columns with data of both tables as a final result.

Any ideas?

  • 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-29T20:46:57+00:00Added an answer on May 29, 2026 at 8:46 pm

    I am not sure if I understand your requirement 100%, but this:

    row2.ItemArray.Where(r2 => row1.ItemArray.Contains(r2) == false)
    

    will filter out those items that happen to appear in any column of table 1, not just the column you are joining on.

    So what I would try to do is filter the item based on the index, using an overload of the Where extension method:

    // Get the index of the column we are joining on:
    int joinColumnIndex = dt2.Columns.IndexOf("Code");
    
    // Now we can filter out the proper item in the rowData query:
    row2.ItemArray.Where((r2,idx) => idx != joinColumnIndex)
    

    …

    No, wait. Here:

    var dt2FilterQuery = from dc in dt2Query.AsEnumerable()
                         where targetTable.Columns
                               .Contains(dc.ColumnName) == false
                         select dc;
    

    You are filtering out all columns of table 2 whose name also appear in table 1. So what you probably want is this:

    public static DataTable DataTableJoiner(DataTable dt1, DataTable dt2)
    {
        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)
                             select dc;
    
        var columnsToAdd = dt2FilterQuery.ToArray();
        var columnsIndices = columnsToAdd.Select(dc => dt2.Columns.IndexOf(dc.ColumnName));
    
        targetTable.Columns.AddRange(columnsToAdd);
    
        var rowData = from row1 in dt1.AsEnumerable()
                          join row2 in dt2.AsEnumerable()
                          on row1.Field<int>("Code") equals 
                             row2.Field<int>("Code")
                          select row1.ItemArray
                              .Concat(row2.ItemArray
                              .Where((r2,idx) => 
                                  columnsIndices.Contains(idx))).ToArray();
    
        foreach (object[] values in rowData) targetTable.Rows.Add(values);
        return targetTable;
    }
    

    Btw. I don’t quite understand why you are wrapping the DataTable you return in a using statement. Imho it is kind of pointless to dispose the very object you return to your caller right away…

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

Sidebar

Related Questions

When I try to develop CRM stuff, using the code below: public static CrmService
i am using this code below to dynamically capture the name of the button
Hi I'm using this code below to let a user draw their signature on
I tried to send an email using this class below, but no success, no
I'm using this code, and I get the stack trace that is listed below.
I have a class that I am using below. And I am using this
I've created a stored procedure similar to the one below (I'm using this cut
I have a problem when trying to execute this update statement (below) using C#
Below is my Oracle Procedure. When i call this procedure using java, it throws
Thanks for reading this. I have markup similar to what is below. Using the

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.