0

my SQL query is

select id,name from 
table1 tbl
where
tbl.id not in (select id from table2 where institutionid = 12011);

I need a linq query for this. I tried several queries. but cannot get the result.

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
siva
  • 525
  • 2
  • 14

2 Answers2

1

I prefer use Contains:

var q = from tb1 in table1
        where !table2.where(y => y.institutionid == 12011)
                     .select(z => z.id).Contains(tb1.id)
        select new { tb1.id, tb1.name }; 
Tim.Tang
  • 3,158
  • 1
  • 15
  • 18
0

Use join/outer join, then test if join is null. The only thing odd is the DefaultIfEmpty().Take(1) "technique" to get you an outer join to one record or nothing. Otherwise, it's pretty clean syntax.

from tb1 in table1
join tb2 in table2 on new {tb1.id,institutionid = 12011} equals new { tb2.id, institutionid} into tb2s
from test in tb2s.DefaultIfEmpty().Take(1)
where test==null
select new { id,name}
kimbonics
  • 81
  • 4