Search code examples
sqlentity-frameworklinq-query-syntax

Entity Framework Query for Complex Sql


Anyone know how to represent this as an Entity Framework query? Either method syntax or query syntax is fine.

Declare @UserId int = 18

Select *
From [Profiles]
Where [Profiles].[ProfileId] 
IN( Select [SecurityProfileAssignments].[ProfileId]
    From [SecurityProfileUsers]
    Join [SecurityProfileAssignments] On [SecurityProfileAssignments].[SecurityProfileId] = [SecurityProfileUsers].[SecurityProfileId]
    Where [SecurityProfileUsers].[UserId] = @UserId)

Solution

  • Given a variable userId:

    from p in Profiles
    join spa in SeucrityProfileAssignments
        on p.ProfileId equals spa.ProfileId
    join spu in SecurityProfileUsers
        on spa.SecurityProfileId equals spu.SecurityProfileId
    where spu.UserId = userId
    select new //optional object type
    {
        //values to select
    }