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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:44:01+00:00 2026-06-03T19:44:01+00:00

I know how to do this in sql, but for c#..I cannot figure out

  • 0

I know how to do this in sql, but for c#..I cannot figure out how to make comparison of two datatables.

Let’s say:

1st datatable:

Name  |  Balance | Description
Smith |   1200   | Smith owes 600
Jordan|   4000   | Hi Jordan
Brooks|   5000   | I like my cat
Navaro|   6000   | description here
Gates |   9010   | omg

2nd datatable:

Name  |  Balance | Description
Smith |   1600   | Smith owes 600
Jordan|   4200   | I'M JORDAN
Clay  |   9000   | Test description
Brooks|   5000   | I like my cat

I want to dump results of comparison to a simple html table.

Soooo…result should be like this:
enter image description here

Basically what I need is:

  1. Show columns that are different and show data

  2. Do not show record if all columns are identical

  3. Show records that only exist in first datatable (just names)

  4. Show records that only exist in second datatable (just names)

In sql, you could do something like merge, then pivot.

But in C#,
my findings:
I can use except, or intersect, but it returns one dattable. Is there any formatting options for an except\intersection functions?

I’m looking for an advice on how to achieve this the best way. (there’s about 100 columns in each datatable). All should be compared by name.

  • 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-03T19:44:02+00:00Added an answer on June 3, 2026 at 7:44 pm

    Here is the code you need to have in your .cs file…

    (I’ve only created those two empty classes to avoid having in code Dictionary<object, Dictionary<string, Tuple<object, object>>> but you can replace that if you prefer)

    protected class Differences : Dictionary<object, RowDifferences>
    {
    }
    
    protected class RowDifferences : Dictionary<string, Tuple<object, object>>
    {
    }
    
    protected Differences GetDifferences(DataTable table1,
                                         DataTable table2,
                                         out IEnumerable<object> onlyIn1,
                                         out IEnumerable<object> onlyIn2)
    {
        var arr1 = new DataRow[table1.Rows.Count];
        var arr2 = new DataRow[table2.Rows.Count];
    
        table1.Rows.CopyTo(arr1, 0);
        table2.Rows.CopyTo(arr2, 0);
    
        onlyIn1 = arr1.Where(x1 => arr2.All(x2 => x1[0] != x2[0])).Select(dr => dr[0]);
        onlyIn2 = arr2.Where(x1 => arr1.All(x2 => x1[0] != x2[0])).Select(dr => dr[0]);
    
        var differences = new Differences();
    
        foreach (var x1 in arr1)
        {
            foreach (var x2 in arr2)
            {
                if (x1[0] == x2[0])
                {
                    var rowDifferences = new RowDifferences();
    
                    for (var i = 1; i < x1.ItemArray.Length; i++)
                    {
                        if (x1.ItemArray[i] != x2.ItemArray[i])
                        {
                            rowDifferences.Add(table1.Columns[i].ColumnName,
                                               new Tuple<object, object>(x1.ItemArray[i], x2.ItemArray[i]));
                        }
                    }
    
                    differences.Add(x1[0], rowDifferences);
                }
            }
        }
    
        return differences;
    }
    
    protected void GenerateTables(out DataTable table1, out DataTable table2)
    {
        table1 = new DataTable();
        table2 = new DataTable();
    
        table1.Columns.Add("Name");
        table1.Columns.Add("Balance");
        table1.Columns.Add("Description");
    
        table2.Columns.Add("Name");
        table2.Columns.Add("Balance");
        table2.Columns.Add("Description");
    
        table1.Rows.Add("Smith", 1200, "Smith owes 600");
        table1.Rows.Add("Jordan", 4000, "Hi Jordan");
        table1.Rows.Add("Brooks", 5000, "I like my cat");
        table1.Rows.Add("Navaro", 6000, "description here");
        table1.Rows.Add("Gates", 9010, "omg");
    
        table2.Rows.Add("Smith", 1600, "Smith owes 600");
        table2.Rows.Add("Jordan", 4200, "I'M JORDAN");
        table2.Rows.Add("Clay", 9000, "Test description");
        table2.Rows.Add("Brooks", 5000, "I like my cat");
    }
    

    And here is an example of how to build the table out of it in the .aspx file:

    <%
        DataTable table1, table2;
        GenerateTables(out table1, out table2);
    
        IEnumerable<object> onlyIn1, onlyIn2;
        var differences = GetDifferences(table1, table2, out onlyIn1, out onlyIn2);
    %>
    
    <table>
        <thead>
            <tr>
                <th>Name</th> 
                <th>RecordName</th> 
                <th>1st Datatable</th> 
                <th>2nd Datatable</th> 
            </tr>
        </thead>
        <tbody>
            <%
                foreach (var difference in differences)
                {
            %>
            <tr>
                <td><%=difference.Key%></td>
            </tr>
            <%
                    foreach (var rowDifferences in difference.Value)
                    {
            %>
            <tr>
                <td></td>
                <td><%=rowDifferences.Key%></td>
                <td><%=rowDifferences.Value.Item1%></td>
                <td><%=rowDifferences.Value.Item2%></td>
            </tr>
            <%
                    }
                }
            %>
            <tr>
                <td>Only 1st datatable</td>
            </tr>
            <%
                foreach (var name in onlyIn1)
                {
            %>
            <tr>
                <td><%=name%></td>
            </tr>
            <%
                }
            %>
            <tr>
                <td>Only 2st datatable</td>
            </tr>
            <%
                foreach (var name in onlyIn2)
                {
            %>
            <tr>
                <td><%=name%></td>
            </tr>
            <%
                }
            %>
        </tbody>
    </table>
    

    Styling the table as you wish shouldn’t be hard from here on.

    So the main thing you have left is changing GenerateTables to some querying logic, or even in-line it inside GetDifferences.

    The finding algorithm can probably be perfected. It is currently O(m * n * k) in the worst case scenario, m and n being the number of rows in table1 and table2 respectively, and k being the number of columns. I can already think of ways to improve it by much, but I’ll leave those to you. This should get you started nice and good.

    Just note that this algorithm assumes that the columns are equal between the two tables.

    Let me know if there’s anything unclear about the solution, and good luck!

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

Sidebar

Related Questions

Know this might be rather basic, but I been trying to figure out how
I know SQL well but I must be missing something really dumb here. This
I'd like to know the most efficient SQL query for achieving this problem: Say
I know this has probably been answered before, but I cannot find much information
I started using NHibernate today, but I cannot figure out how I setup a
Hey guys I know that this is probably just a simple sql statement but
Does anyone know how I can reproduce this SQL Query as a SubSonic Query?
I know how to create a job dynamically from this create-sql-server-job-automatically The issue I
I know this is fairly subjective, but I'm diving into testing and learning about
I know this is largely an opinion, but I'm interested if you have one

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.