简体   繁体   English

在C#中的LINQ中进行JOIN

[英]Doing a JOIN in LINQ in C#

I have the following C# code 我有以下C#代码

List<User> users = User.FindAll();
List<Role> roles = Role.FindAll();

These are custom security related objects. 这些是与自定义安全性相关的对象。 The properties on the two objects are shown here: 这两个对象的属性如下所示:

User                       Role
----                       ----
UserId                     RoleId
UserName                   RoleName
RoleIds

I am trying to get create a new User object that will give me the RoleIds and RoleNames for each user. 我试图创建一个新的User对象,该对象将为我提供每个用户的RoleIds和RoleNames。 Essentially, I want a new User object that will look like this: 本质上,我想要一个新的User对象,该对象将如下所示:

User
----
UserId
UserName
Roles (RoleId, RoleName)

I need to do this in memory. 我需要在内存中执行此操作。 Currently, I have the following: 目前,我有以下内容:

var results = (from user in users
               select new
               {
                 UserId = user.UserId,
                 UserName = user.UserName,
               });

However, I do not know how to add the Roles property such that only a list of Roles associated with each user are included. 但是,我不知道如何添加Roles属性,以便仅包括与每个用户关联的Roles列表。 I do not know how to do a join with the roles variable. 我不知道如何使用roles变量进行联接。 Is there a way to do this with LINQ? 有没有办法用LINQ做到这一点? If so, how? 如果是这样,怎么办?

Use Where Where使用

var results = (from user in users
           select new
           {
             UserId = user.UserId,
             UserName = user.UserName,
             Roles = roles.Where(r => user.RoleIds.Contains(r.RoleId)).ToList() 
           });

This assumes that RoleIds is in fact an array containing RoleIds. 这假定RoleIds实际上是一个包含RoleIds的数组。 If that's not the case and it is, say, a comma separate string of RoleIds then you would need to string.split it first to create an array of role Ids and then Contains would have the expected behavior (ie not matching "1" against "10". 如果不是这种情况,例如用逗号分隔的RoleId字符串,则需要将string.split拆分以创建角色ID数组,然后Contains将具有预期的行为(即,不匹配“ 1” “ 10”。

Have you try this: 您是否尝试过以下方法:

Create a Class 建立课程

  Public Class tblUser
    {
    public string UserId  {get; set;} 
    public string UserName  {get; set;} 
    public string RoleIds {get; set;} 
    public string RoleName {get; set;}                   
    }

var results = (from h in users
               join  n Role on h.RoleIds  equal  n.RoleId
                 select new tblUser
                  {
                    UserId = h.UserId,
                    UserName = h.UserName,
                    RoleName  = n.RoleName 
                  }
               );

Hope it will help !!!!!! 希望对您有所帮助!

What I would suggest is this: Refactor your User/Role relationship in the database so that you have three tables. 我的建议是:在数据库中重构用户/角色关系,以便拥有三个表。

User
-----
UserId
UserName


Role
-----
RoleId
RoleName


UsersInRoles
-----
UserId
RoleId
IsActive

Then, have foreign key relationships from UsersInRoles to User and UsersInRoles to Role on the Id columns. 然后,在ID列上具有从UsersInRoles到User和UsersInRoles到Role的外键关系。 Then, when you query, you can join all three tables together to get the necessary information, like so: 然后,在查询时,可以将所有三个表连接在一起以获取必要的信息,如下所示:

var results = (from u in Users
               join uir in UsersInRoles on u.UserId equals uir.UserId
               join r in Role on uir.RoleId equals r.RoleId
               where uir.IsActive
               select new
               {
                   UserId = u.UserId,
                   UserName = u.UserName,
                   RoleName  = r.RoleName
               });

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

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