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