1

I am new to LINQ. I am confusing to query it. Please any one tell me how to convert the below query in LINQ

select * from tbldev 
where iddevice not in(select a.iddevice from 
tblUDMap a join tbldev d 
on a.iddevice=d.iddevice )
Boss
  • 445
  • 2
  • 8
  • 24

4 Answers4

2

A literal and naive word to word translation would be something like this:

    var result = from dev in context.tbldev
                 where (from udmap in context.tblUDMap join dev2 in context.tbldev on udmap.iddevice equals dev2.iddevice select udmap.iddevice)
                       .Contains(dev.iddevice) == false
                 select dev.iddevice;  

But your question is strange! You jon the two tables and then filterout those rows in tbldev that were successfully participated in join! It means you want those rows in tbldev which cannot be joint to tblUDMap. So it is more simple to write this:

var result = from dev in context.tbldev 
             where (from udmap in context.tblUDMap 
                    where udmap.iddevice == dev.iddevice
                    ).Count() == 0
             select dev.iddevice;
Alireza
  • 10,237
  • 6
  • 43
  • 59
2

There's no need for nested queries here:

from x in tbldev
join y in tblUDMap
    on x.iddevice equals y.iddevice
    into grp
where !grp.Any()
select x

This will select all records from tbldev for which there are no corresponding records in tblUDMap.

Thom Smith
  • 13,916
  • 6
  • 45
  • 91
2

You could simplify your query to something like this

var query = tbldev.Where(e => !tblUDMap.Any(a => a.iddevice == e.iddevice))
Esteban Elverdin
  • 3,552
  • 1
  • 17
  • 21
1

try this:

from res in tbldev
where !(from a in tblUDMap
        join b in tbldev on a.Iddevice equals b.iddvice
        into c
        select c)
select res

The important part is the negated where-clause. the join syntax in this might not be correct, from the top of my head.

Marco
  • 22,856
  • 9
  • 75
  • 124