简体   繁体   中英

How to write nested query in Linq

i have got two table

  1. usermaster
  2. AreaMaster

my sql query is like this:

select * from tblArea  where areaid
not in (select areaid from
tblUserMaster)

please tell me how to write such nested query in linq

var result = tblArea.Where(x => !tblUserMaster.Any(m => m.areaid == x));

I can't find any good way you can do something like:

var tb1 = tblUserMaster.ToList();
var result = tblArea.AsEnumerable().Where(x => !tb1.Any(m => m.areaid == x));

but it's not a good way, It's better to write store procedure instead loading all data to client. may be someone can do this better but I think it can not be improved.

AsEnumerable() keyword moves from linq->sql to pure linq but it loads all data.

try this one

var result=from tblarea in db.TblArea
where
  !
    (from tblusermaster in db.TblUserMaster
    select new {
      tblusermaster.Areaid
    }).Contains(new { tblarea.Areaid })
select new {
  tblarea.Areaid,
  tblarea.Column1,
  tblarea.Column2
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM