1

I have two DataTables. I can join these two tables using LINQ but I am trying to write the following SQL query to LINQ but failed.

What is did for joining two DataTables:

dtMerged = (from a in dtOraDataTable.AsEnumerable()
                join b in dtSqlSmsTable.AsEnumerable()
                    on a["EntrySerialNo"].ToString() equals b["EntrySerialNo"].ToString()
                    into g
                where g.Any()
                select a).CopyToDataTable();

Now I want to do:

SELECT * FROM OraSmsData a WHERE EntrySerialNo NOT IN (SELECT EntrySerialNo FROM SqlSmsData WHERE IsProcessed=1);

How can I implement this in LINQ? I will appreciate if anyone guide me here.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

2 Answers2

3

Does this work for you?

var query =    
    from a in dtOraDataTable.AsEnumerable() 
    where !(from b in dtSqlSmsTable.AsEnumerable()   
            select b["EntrySerialNo"].ToString())    
           .Contains(a["EntrySerialNo"].ToString()    
    select a;
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
0

You can do something as below:

var query = 
  dtOraDataTable.AsEnumerable()
    .Where(x => !dtSqlSmsTable.AsEnumerable()
    .Select(y => y.Field<decimal>("EntrySerialNo")).Contains(x.Field<decimal>("EntrySerialNo")));
Sowmyadhar Gourishetty
  • 1,843
  • 1
  • 8
  • 15
  • "EntrySerialNo" is sql server numeric datatype. I am getting conversion error. – Sajid Wasim Sep 08 '20 at 16:11
  • Does `int` solved your problem? You can either use `decimal` or `double` if you still get that error. – Sowmyadhar Gourishetty Sep 08 '20 at 16:18
  • This works for me. Thanks @Sowmyadhar. var dtMerged = dtOraDataTable.AsEnumerable() .Where(x => !dtSqlSmsTable.AsEnumerable() .Select(y => y.Field("EntrySerialNo")).Contains(x.Field("EntrySerialNo"))).CopyToDataTable(); – Sajid Wasim Sep 08 '20 at 16:19