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

  • Home
  • SEARCH
  • 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 7867905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:50:06+00:00 2026-06-03T00:50:06+00:00

I am using DataRelation to compare two DataTable and return a third table containing

  • 0

I am using DataRelation to compare two DataTable and return a third table containing the rows that are not share by both original tables by using this tutorial. Below is the code.

public static DataTable Difference(DataTable First, DataTable Second)
{
      //Create Empty Table

      DataTable table = new DataTable("Difference");

      //Must use a Dataset to make use of a DataRelation object

      using(DataSet ds = new DataSet())   
      {
            //Add tables
            ds.Tables.AddRange(new DataTable[]{First.Copy(),Second.Copy()});
            //Get Columns for DataRelation

            DataColumn[] firstcolumns  = new DataColumn[ds.Tables[0].Columns.Count];   
            for(int i = 0; i < firstcolumns.Length; i++)   
            {    
                  firstcolumns[i] = ds.Tables[0].Columns[i];    
            }

            DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];

            for(int i = 0; i < secondcolumns.Length; i++)    
            {
                  secondcolumns[i] = ds.Tables[1].Columns[i];    
            }    
            //Create DataRelation    
            DataRelation r = new DataRelation(string.Empty,firstcolumns,secondcolumns,false);

            ds.Relations.Add(r);

            //Create columns for return table   
            for(int i = 0; i < First.Columns.Count; i++)    
            {    
                  table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);    
            }

            //If First Row not in Second, Add to return table.    
            table.BeginLoadData();

            foreach(DataRow parentrow in ds.Tables[0].Rows)    
            {    
                  DataRow[] childrows = parentrow.GetChildRows(r);

                  if(childrows == null || childrows.Length == 0)    
                        table.LoadDataRow(parentrow.ItemArray,true);        
            }

            table.EndLoadData();    
      }

      return table;    
}

Surprisingly enough when I exercised this code, it only worked on few cases and failed in others.

There was one case, I was comparing two identical table (but has different source) that both has:

  • Same Column Name & DataType
  • Same Number of Rows & Columns
  • Same Value in Each Cells

Only to have a brand new table returned that actually has the exact same composition as the two original tables!

What did I possibly miss?

Is it possible that two identical tables (sharing features I mentioned above) can have other different properties (which doesn’t seem visible to users’ eyes)?

Or is it possible that this is actually a bad method? What are the possible alternatives?

EDITED

  1. Both tables have same primitive datatype such as: System.String, System.Int32, System.DateTime
  2. These code doesn’t work on all samples I tested
  3. Here is a print screen of 1 sample (using DataSet Visualizer)

enter image description here

  • 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-03T00:50:07+00:00Added an answer on June 3, 2026 at 12:50 am

    I had to write something similar once and this is the approach I used:

    First this approach only works if you don’t have duplicate rows in each table.

    Using primary keys..

    First.PrimaryKey = firstcolumns;
    Second.PrimaryKey = secondcolumns; //These throw exceptions when you have duplicate rows
    

    Then..

    foreach (DataRow dr in Second.Rows)
    {
        List<Object> l = new List<Object>();
    
        foreach (DataColumn dc in secondcolumns) l.Add(dr[dc]);
    
        if (First.Rows.Find(l.ToArray()) == null) //NOT FOUND
        {
            table.Rows.Add(l.ToArray());
        }
    }
    
    foreach (DataRow dr in First.Rows)
    {
        List<Object> l = new List<Object>();
    
        foreach (DataColumn dc in firstcolumns) l.Add(dr[dc]);
    
        if (Second.Rows.Find(l.ToArray()) == null) //NOT FOUND
        {
            table.Rows.Add(l.ToArray());
        }
    }
    

    Cheers,

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

Sidebar

Related Questions

I want to show a master / detail relationship using two datagridviews and DataRelation
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using Browserlab, it appears that the background image is not centred in Firefox7 for
Using Nunit, I want to be able to write a test fixture that will
Using PHP, I can convert MySQL data or static table data to csv, Excel,
Using top it's easy to identify processes that are hogging memory and cpu, but
Using the javascript function function squareIt(number) { return number * number; } When given
I'd like to know what the advantage of using a DataRelation in .NET is,
I have a table that could have thousands (millions maybe?) of records. It is
using the arcsynthesis opengl tutorial and I'm trying to build the unofficial sdk that

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.