0

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

0

3 Answers 3

4
var result = tblArea.Where(x => !tblUserMaster.Any(m => m.areaid == x));
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't work in linq to sql, just primitive types can be done
i have checked and its not working. its generating exception when i try to use foreach on it: Could not format node 'New' for execution as SQL.
thanks! its work when i have written: var result = tblArea.Where(x => !tblUserMaster.Any(m => m.areaid == x.areaid));
1

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.

2 Comments

@Rajesh Rolen- DotNet Developer, yes with tblUserMaster.ToList().Any(m => m.areaid == x) works
Now its working when i have written: var tb1 = tblUserMaster.ToList(); var result = tblArea.AsEnumerable().Where(x => !tb1.Any(m => m.areaid == x.areaid));
1

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
}

1 Comment

What error are you getting, I tried a working version of this one in my machine and it worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.