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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:25:23+00:00 2026-05-22T21:25:23+00:00

what would be the best way to compare two data table. i populate two

  • 0

what would be the best way to compare two data table. i populate two data table reading two different xml and now i need to compare and return the difference in terms of datatable.my compare logic was

private DataTable CompareDataTables(DataTable dtFirst, DataTable dtSecond)
    {
        int result = 0;
        bool flag = false;
        DataTable dtNoRows = new DataTable();
        dtNoRows.Columns.Add("Result");

        DataTable dtDiff = new DataTable();
        dtDiff.Columns.Add("Field Name");
        dtDiff.Columns.Add("Old Value");
        dtDiff.Columns.Add("New Value");
        DataRow dr = null;
        if (dtFirst.Columns.Count == dtSecond.Columns.Count)
        {
            for (int i = 0; i <= dtFirst.Columns.Count - 1; i++)
            {
                try
                {
                    DateTime.Parse(dtFirst.Rows[0][dtFirst.Columns[i].ColumnName.ToString()].ToString().Trim());
                    flag = true;
                }
                catch (Exception ex)
                {
                    flag = false;
                }

                if (!flag)
                {
                    if (dtFirst.Rows[0][dtFirst.Columns[i].ColumnName.ToString()].ToString().Trim().ToUpper() != dtSecond.Rows[0][dtSecond.Columns[i].ColumnName.ToString()].ToString().Trim().ToUpper())
                    {
                        dr = dtDiff.NewRow();
                        dr["Field Name"] = dtFirst.Columns[i].ColumnName.ToString();
                        dr["Old Value"] = dtFirst.Rows[0][dtFirst.Columns[i].ColumnName.ToString()].ToString().Trim();
                        dr["New Value"] = dtSecond.Rows[0][dtSecond.Columns[i].ColumnName.ToString()].ToString().Trim();
                        dtDiff.Rows.Add(dr);
                    }
                }
                else
                {
                    result = DateTime.Compare(DateTime.Parse(dtFirst.Rows[0][dtFirst.Columns[i].ColumnName.ToString()].ToString()), DateTime.Parse(dtSecond.Rows[0][dtSecond.Columns[i].ColumnName.ToString()].ToString()));
                    if (result != 0)
                    {
                        dr = dtDiff.NewRow();
                        dr["Field Name"] = dtFirst.Columns[i].ColumnName.ToString();
                        dr["Old Value"] = DateTime.Parse(dtFirst.Rows[0][dtFirst.Columns[i].ColumnName.ToString()].ToString().Trim()).ToString("MM/dd/yyyy") + " - " + DateTime.Parse(dtFirst.Rows[0][dtFirst.Columns[i].ColumnName.ToString()].ToString().Trim()).ToString("hh:mm:ss");
                        dr["New Value"] = DateTime.Parse(dtSecond.Rows[0][dtSecond.Columns[i].ColumnName.ToString()].ToString().Trim()).ToString("MM/dd/yyyy") + " - " + DateTime.Parse(dtSecond.Rows[0][dtSecond.Columns[i].ColumnName.ToString()].ToString().Trim()).ToString("hh:mm:ss");
                        dtDiff.Rows.Add(dr);

                    }
                    flag = false;
                }
            }
        }
        return dtDiff;
    }

my code is working fine but i need to the is there any best way out. please guide 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-22T21:25:24+00:00Added an answer on May 22, 2026 at 9:25 pm

    you can use LINQ to comparing tables values(two table must have the same structure)

    bool flag = false;
    if (dtFirst.Columns.Count == dtSecond.Columns.Count)
    {
        for (int i = 0; i <= dtFirst.Columns.Count - 1; i++)
        {
            String colName = dtFirst.Columns[i].ColumnName;
            var colDataType = dtFirst.Columns[i].DataType.GetType();
            var colValue = dtFirst.Columns[i];
            flag = dtSecond.AsEnumerable().Any(T => typeof(T).GetProperty(colName).GetValue(T, typeof(colDataType)) == colValue);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the best way to compare two date in java? Right now
I need to very efficiently compare two maps in Clojure/Java, and return the difference
Possible Duplicate: What is the best way to compare two entity framework entities? I
I've been reading up on strtok and thought it would be the best way
I need to compare two datetime values to determine equality(exactly the same),using minute precision.Would
I'm looking for an efficient way to compare and get differences between two XML-based
What is the best way to compare two strings to see how similar they
What is the best way to compare two paths in .Net to figure out
What would be the best way to compare files and or directories. Lets say
What would be the best way to compare a gesture made on an Android

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.