简体   繁体   English

使用JOIN到LINQ的SQL查询

[英]SQL query with JOIN to LINQ

I'm having trouble translating following query to Linq: 我在将以下查询转换为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 我尝试了多个Linq查询,但不确定如何建立Roles <-> Users关系,以便可以从每个用户那里获取角色,EF使用aspnet_UsersInRoles表在他们之间创建了一个映射

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. 你可以看一下更详细讲述LINQ查询加盟这里的MSDN文档。

Same as Jenish, but if Roles is a navigation property of Users you can avoid a Join 与Jenish相同,但是如果Roles是Users的导航属性,则可以避免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 导航属性aspnet_Role可能具有其他名称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): 基于EF生成aspnet_UsersInRolesaspnet_UsersInRoles和您在所需的,生成的SQL查询中提到的某些字段名称,我假设POCO与以下类似(此答案的其余部分取决于这些假设):

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: 使用此设置,您应该能够通过以下方式获得所需的SQL输出:

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. 请注意,EF和L2E可以为您做所有的魔术:它甚至可以从关系名称中推断出两个POCO之间的关系。

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: 仅使用上面的代码,我将其放入了编译器,将var q设置为与显示的查询相等,然后将q.ToString()转储了出来-这就是我得到的:

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. 我认为这几乎可以抓住它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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