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

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,372 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,164 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 26,201 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 26,201 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