T-SQL Query
Select * from dbo.User_Users
Where UserID IN (Select UserID from Course_Enrollments)
LINQ to Entities alternative of Above Quer
from u in User_Users
where u.Course_Enrollments.Any()
select u
if you have the foreign keys set up. If not you can do this
from u in User_Users
join cu in Course_Enrollments on u.UserId equals cu.UserId
select u
You also should wrap any of these with .Distinct()
call
Simple answer is use the "let" keyword and generate a sub-query that supports your conditional set for the main entity.
var usersEnrolledInCourses = from u in User_Users
let ces = from ce in Course_Enrollments
select ce.UserID
where ces.Contains(u.UserID)
select u;
This will create an exists block in TSQL similar to
SELECT [Extent1].*
FROM dbo.User_Users AS Extent1
WHERE EXISTS (SELECT 1 AS [C1]
FROM dbo.Course_Enrollements AS Extent2
WHERE (Extent2.UserID = Extent1.UserId))
It's close to what you've asked for and will generally create the same query plan on SQL Server.
Hope this helps!