SQL Subquery in LINQ for Entity Framework 4.0
Posted
by Jorin
on Stack Overflow
See other posts from Stack Overflow
or by Jorin
Published on 2010-05-03T22:34:33Z
Indexed on
2010/05/03
22:38 UTC
Read the original article
Hit count: 1162
linq-to-entities
|sql-server
I'm new to LINQ and EF, but I've been able to stumble through for the majority of the queries I have, but this one has me completely confused. No matter what I try, it comes up in SQL Profiler as a big mess :-).
I have two tables: Users and UsersProjects. The goal of this query is to list all the users who are working on projects with the specified user. Here is the query as I have it written in SQL. It's a subquery, but I don't know of a way to simplify it further, but I'm open to suggestions there as well.
SELECT DISTINCT Users.FirstName, Users.LastName
FROM Users INNER JOIN UsersProjects ON Users.ID=UsersProjects.UserID
WHERE UsersProjects.ProjectID IN
(SELECT ProjectID FROM UsersProjects WHERE UserID=@UserID)
Anybody able to help?? It seems like a fairly simple subquery in SQL, but in LINQ, I'm baffled.
Thanks,
Jorin
© Stack Overflow or respective owner