Comparing two datatables and returning a datatable by matching one or more columns using LINQ in C#

You can compare two datatables by matching the columns and perform minus operations as you do in RDBMS.

using System.Linq;

public DataTable getLinq(DataTable dt1, DataTable dt2)
 {
   DataTable dtMerged = 
        (from a in dt1.AsEnumerable()
                join b in dt2.AsEnumerable()
                                  on 
        a["Query"].ToString() equals b["Query"].ToString()
                                into g
                      where g.Count() > 0
               select a).CopyToDataTable();
            return dtMerged;
 }


Working example:

Consider datatable dt1 is having the data
Placeidtime
Russia122:10
Italy223:10
Australia200:10
India101:10
Pakistan602:10
Sri lanka103:10
China404:10
London105:10
Germany606:10
Iraq107:10
America408:10
India109:10

Consider datatable dt2 is having the data
Place
India
Iraq
Italy


Now we are going to perform MINUS operation. We are going to retrieve the rows from dt1 which has the places in dt2.

public DataTable getLinq(DataTable dt1, DataTable dt2)
        {
            DataTable dtMerged = (from a in dt1.AsEnumerable()
                                  join b in dt2.AsEnumerable()
                                  on a["Place"].ToString() equals b["Place"].ToString()
                                  into g
                                  where g.Count() > 0
                                  select a).CopyToDataTable();

            return dtMerged;
        }


Result set :
Placeidtime
Italy230-12-1899 23:10
India101-01-1900 01:10
Iraq101-01-1900 07:10
India101-01-1900 09:10