Compare 2 datatable Cells and return boolean values in c#

Suresh S 96 Reputation points
2022-08-01T13:23:15.923+00:00

Hi All,

Please provide your inputs the feasible way to compare two datatables in C#.

Datatable1:

EmpID EmpName Age
1 AAA 22
2 BBB 23
3 CCC 25

DataTable2:

EmpID EmpName Age
1 AAA 20
2 BBB 23
3 CCD 25

Result datatable should be as given below:

EmpID EmpName Age
True True False
True True True
True False True

Thanks in advance.
Suresh Sankaran

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-08-01T13:31:03.053+00:00

    Typically, a SQL query is used to work with set data not a DataTable. Can you explain the use case or the problem you are trying to solve?

    class Program  
    {  
        static void Main(string[] args)  
        {  
      
            DataTable table1 = PopulateDataTable();  
            DataTable table2 = PopulateDataTable2();  
      
            var query = from t1 in table1.AsEnumerable()  
                        join t2 in table2.AsEnumerable()  
                        on t1.Field<int>("EmpId") equals t2.Field<int>("EmpId")  
                        select new CompareDataTablesResults()  
                        {  
                            EmpID = t1.Field<int>("EmpId"),  
                            EmpName = t1.Field<string>("EmpName") == t2.Field<string>("EmpName"),  
                            Age = t1.Field<int>("Age") == t2.Field<int>("Age")  
                        };  
      
            foreach(var item in query)  
            {  
                Console.WriteLine($"{item.EmpID}\t{item.EmpName}\t{item.Age}");  
            }  
      
        }  
      
        public static DataTable PopulateDataTable()  
        {  
            DataTable table = new DataTable();  
            table.Columns.Add("EmpID", typeof(int));  
            table.Columns.Add("EmpName", typeof(string));  
            table.Columns.Add("Age", typeof(int));  
      
            table.Rows.Add(1, "AAA", 22);  
            table.Rows.Add(2, "BBB", 23);  
            table.Rows.Add(3, "CCC", 25);  
      
            return table;  
        }  
      
        public static DataTable PopulateDataTable2()  
        {  
            DataTable table = new DataTable();  
            table.Columns.Add("EmpID", typeof(int));  
            table.Columns.Add("EmpName", typeof(string));  
            table.Columns.Add("Age", typeof(int));  
      
            table.Rows.Add(1, "AAA", 20);  
            table.Rows.Add(2, "BBB", 23);  
            table.Rows.Add(3, "CCC", 25);  
      
            return table;  
        }  
      
    }  
    

    Results

    1       True    False  
    2       True    True  
    3       True    True  
    
    0 comments No comments

  2. Suresh S 96 Reputation points
    2022-08-02T04:47:21.767+00:00

    Hi AgaveJoe,

    Thank you for the support.

    I need more clarity on the statement "CompareDataTablesResults()".

    var query = from t1 in table1.AsEnumerable()
    join t2 in table2.AsEnumerable()
    on t1.Field<int>("EmpId") equals t2.Field<int>("EmpId")
    select new CompareDataTablesResults()
    {
    EmpID = t1.Field<int>("EmpId"),
    EmpName = t1.Field<string>("EmpName") == t2.Field<string>("EmpName"),
    Age = t1.Field<int>("Age") == t2.Field<int>("Age")
    };
    This is statement shows an error. I have removed the CompareDataTablesResults() and tried. It is working but the "query" is empty.


  3. AgaveJoe 30,126 Reputation points
    2022-08-02T10:12:40.61+00:00

    I need more clarity on the statement "CompareDataTablesResults()".

    CompareDataTablesResults is a class.

        public class CompareDataTablesResults  
        {  
            public int EmpID { get; set; }  
            public bool EmpName { get; set; }  
            public bool Age { get; set; }  
        }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.