Linq query to subtract two datatable and return all the rows in first data table

Hi,

Example -
Data table 1 is as below
Column A. Column B.

  1.                  A
    
  2.                 B
    
  3.                 C
    
  4.                 D
    
  5.                 E
    

Data table 2 is as below
Column A. Column B.

  1.                 A
    
  2.                C
    

Output data table should be

Column A. Column B.
2. B
4. D
5. E

Any idea how to achieve it using LINQ query?

Thanks in advance!

Hi,

How about the following expression?

dtResult = dt1.AsEnumerable.Except(dt2.AsEnumerable,DataRowComparer.Default).CopyToDataTable()

Regards,

Hi @Ray_Sha1,

Try below query -

(From d1 In dt1.AsEnumerable()
From d2 In dt2.AsEnumerable()
Where Not d1(1).toString.Equals(d2(1).toString.Trim)
Let var = New Object(){d1(0), d2(0)}
Select r = dtResult.Rows.Add(var)).CopyToDataTable

Thanks

Hi @Shikhar_Tandon and @Yoichi

Thanks for the reply.
Both the solutions work fine.

Just a note:
In case the resulting dataset will be empty the “.CopyToDataTable” will fail

Cheers

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.