Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi How can I write the followin tsql query by linq:
SQL
Select * From employees where EmployeeId not in(select EmployeeId from Orders)

Thanks
Posted
Updated 21-May-11 8:13am
v2

1 solution

Generally you can use Linqer[^] to convert any SQL statement to LINQ.

[EDITED]

For your question the tsql will look like this.
C#
var query =
    from e in Employees
    where !(from o in Orders
            select o.EmployeeID)
           .Contains(e.EmployeeID)
    select e;


I hope this helps you well.
 
Share this answer
 
v3
Comments
Wonde Tadesse 21-May-11 14:39pm    
Can anyone tell me what is wrong with this answer ?
NuttingCDEF 22-May-11 9:36am    
As far as I can see, not a lot - though there might be possible optimisations to make it run more efficiently. 2 suggestions below - I haven't tested them, so if they don't work, think about the concepts rather than the exact detail :-)


Suggestion 1 (run the Orders query once only and eliminate duplicates from the result)

var IDs = (from o in Orders
select o.EmployeeID).Distinct();

var query =
from e in Employees
where !(IDs.Contains(e.EmployeeID))
select e;


Suggestion 2 (relies on a relationship)

var query =
from e in Employees
where e.Orders.Count() == 0
select e;
Wonde Tadesse 22-May-11 10:34am    
Thank you NuttingCDEF.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900