简体   繁体   中英

SQL query with JOIN to LINQ

I'm having trouble translating following query to Linq:

SELECT u.UserId, 
       u.UserName, 
       r.RoleName 
FROM   aspnet_users u 
       INNER JOIN aspnet_UsersInRoles ur ON u.UserId = ur.UserId 
       INNER JOIN aspnet_Roles r ON r.RoleId = ur.RoleId AND 
                                    r.RoleName IN ('SuperAdmin', 'AdminExtended', 'ExpertExtended') 
ORDER  BY u.UserName 

I've tried with several Linq queries but I'm not sure how to build the Roles <-> Users relation so I can get the role from each user, EF creates a Mapping between them using the aspnet_UsersInRoles table

Here's what I've tried:

var query = from u in context.aspnet_Users
            from r in u.aspnet_Roles
            where r.RoleName == "SuperAdmin" || r.RoleName == "AdminExtended" || r.RoleName == "ExpertExtended"
            select u;

And

var query = from u in context.aspnet_Users
            where u.aspnet_Roles.Any(r => r.RoleName == "SuperAdmin" || r.RoleName == "AdminExtended" || r.RoleName == "ExpertExtended")
            select u;

try like following:

var roleNames = new List<string>(){"SuperAdmin","AdminExtended","ExpertExtended"};
from u in context.aspnet_Users
join uir in context.aspnet_UsersInRoles on u.UserId = uir.UserId
join r in context.aspnet_Roles on r.RoleId = uir.RoleId
where (roleNames.Contains(r.RoleName))
select new {UserId = u.UserId, UserName = u.UserName , RoleName = r.RoleName}

You can look more into detail about linq join query here on msdn documentation.

Same as Jenish, but if Roles is a navigation property of Users you can avoid a Join

from u in context.aspnet_Users
join r in context.aspnet_UsersInRoles  on u.UserId equals r.UserId
select new {u.UserName, r.aspnet_Role.RoleName}

Nav Property aspnet_Role might have a different name IDK

You where close in your first attempt:

from u in context.aspnet_Users
from r in context.aspnet_UsersInRoles
where u.UserId == r.UserId
select new {u.UserName, r.aspnet_Role.RoleName}

Based on EF generating a join table aspnet_UsersInRoles and some of the field names you mention in your desired, generated, SQL query, I assume POCOs similar to the following (the remainder of this answer is dependent on these assumptions):

public class User
{
    public int UserId { get; set; } // just assuming int key ID
    public string UserName { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

With this setup, you should be able to get the SQL output you're looking for with the following:

from r in context.Roles
where r.RoleName == "SuperAdmin" || r.RoleName == "AdminExtended" || r.RoleName == "ExpertExtended"
from u in r.Users
orderby u.UserName
select new {u.UserId, u.UserName, r.RoleName};

Notice EF and L2E does all the magic for you: it even infers the relations between the two POCOs from the relation names.

With just the code above, I dropped this into my compiler, set var q equal to the query shown, and dumped q.ToString() out - this is what I got:

SELECT
    [Extent1].[RoleId] AS [RoleId],
    [Join1].[UserId] AS [UserId],
    [Join1].[UserName] AS [UserName],
    [Extent1].[RoleName] AS [RoleName]
    FROM  [dbo].[Roles] AS [Extent1]
    INNER JOIN  (SELECT [Extent2].[Role_RoleId] AS [Role_RoleId], [Extent3].[UserId] AS [UserId], [Extent3].[UserName] AS [UserName]
        FROM  [dbo].[UserRoles] AS [Extent2]
        INNER JOIN [dbo].[Users] AS [Extent3] ON [Extent3].[UserId] = [Extent2].[User_UserId] ) AS [Join1] ON [Extent1].[RoleId] = [Join1].Role_RoleId]
    WHERE [Extent1].[RoleName] IN (N'SuperAdmin',N'AdminExtended',N'ExpertExtended')
    ORDER BY [Join1].[UserName] ASC

I think this pretty much captures it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM