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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:07:24+00:00 2026-05-25T10:07:24+00:00

I want to make sure that 2 Ado.net datatables have the same schema: number

  • 0

I want to make sure that 2 Ado.net datatables have the same schema: number of columns + col types etc. How can this be done?

Lets say I have variables: Datatable A and Datatable B. How can I compare to see if the schema of A is same as Schema of B

  • 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-25T10:07:24+00:00Added an answer on May 25, 2026 at 10:07 am

    I don’t know of any built in way to compare DataTables and without having defined a complete specification (and you always should) its likely that I’m going to miss some case you care about.

    That said the following does manage to compare two DataTables and determine if the following is true

    • Is the number of data columns the same in both DataTables
    • For each data column in the first dataTable does a column exist in the other table that also is of the same type regardless of order

    It’s using an Extension Method and implements IEqualityComparer to make the comparison.

    Test Cases

    class Program
        {
            static void Main(string[] args)
            {
    
                DataTable dt1 = new DataTable();
                dt1.Columns.Add(columnName: "a", type: Type.GetType("System.String"));
                dt1.Columns.Add(columnName: "b", type: Type.GetType("System.Int32"));
    
                DataTable dt2 = new DataTable();
                dt2.Columns.Add(columnName: "a", type: Type.GetType("System.Int32"));
                dt2.Columns.Add(columnName: "b", type: Type.GetType("System.String"));
    
                DataTable dt3 = new DataTable();
                dt3.Columns.Add(columnName: "a", type: Type.GetType("System.String"));
                dt3.Columns.Add(columnName: "b", type: Type.GetType("System.Int32"));
                dt3.Columns.Add(columnName: "c", type: Type.GetType("System.Int32"));
    
    
                DataTable dt4 = new DataTable();
                dt4.Columns.Add(columnName: "b", type: Type.GetType("System.Int32"));
                dt4.Columns.Add(columnName: "a", type: Type.GetType("System.String"));
    
    
                DataTable dt5 = new DataTable();
                dt5.Columns.Add(columnName: "a", type: Type.GetType("System.String"));
                dt5.Columns.Add(columnName: "b", type: Type.GetType("System.Int32"));
    
    
            Console.WriteLine("dt1.SchemaEquals(dt1) | {0}", dt1.SchemaEquals(dt1));
            Console.WriteLine("dt1.SchemaEquals(dt2) | {0}", dt1.SchemaEquals(dt2));
            Console.WriteLine("dt1.SchemaEquals(dt3) | {0}", dt1.SchemaEquals(dt3));
            Console.WriteLine("dt1.SchemaEquals(dt4) | {0}", dt1.SchemaEquals(dt4));
            Console.WriteLine("dt1.SchemaEquals(dt5) | {0}", dt1.SchemaEquals(dt5));
    
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    Console.ReadLine();
                }
    
    
            }
    
    
        }
    

    Extension Method

        public static class DataTableSchemaCompare
        {
            public static bool SchemaEquals(this DataTable dt, DataTable value)
            {
                if (dt.Columns.Count != value.Columns.Count)
                    return false;
    
                 var dtColumns = dt.Columns.Cast<DataColumn>();
                 var valueColumns = value.Columns.Cast<DataColumn>();
    
    
                var exceptCount =  dtColumns.Except(valueColumns, DataColumnEqualityComparer.Instance).Count() ;
                return (exceptCount == 0);
    
    
            }
        }
    

    Implementation of IEqualityComparer

        class DataColumnEqualityComparer : IEqualityComparer<DataColumn>
        {
            #region IEqualityComparer Members
    
            private DataColumnEqualityComparer() { }
            public static DataColumnEqualityComparer Instance = new DataColumnEqualityComparer();
    
    
            public bool Equals(DataColumn x, DataColumn y)
            {
                if (x.ColumnName != y.ColumnName)
                    return false;
                if (x.DataType != y.DataType)
                    return false;
    
                return true;
            }
    
            public int GetHashCode(DataColumn obj)
            {
                int hash = 17;
                hash = 31 * hash + obj.ColumnName.GetHashCode();
                hash = 31 * hash + obj.DataType.GetHashCode();
    
                return hash;
            }
    
            #endregion
        }
    

    Output

    dt1.SchemaEquals(dt1) | True
    dt1.SchemaEquals(dt2) | False
    dt1.SchemaEquals(dt3) | False
    dt1.SchemaEquals(dt4) | True
    dt1.SchemaEquals(dt5) | True
    Press any key to continue . . .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make sure that a set of functions have the same signature
I am creating an xml schema, and I want to make sure that the
I have a java applet, I want to make sure that nobody use it
I want to make sure that I delete required files. I have code something
I have a client/server program and I want to make sure that its components
I have an event handler, but I want to make sure that it will
I want to make sure that I am using ContinueWhenAll appropriately. I have a
I want to make sure that all of my form fields uses the same
I have a checkbox list for weekdays and I want to make sure that
I have a list of checkboxes and I would want to make sure 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.