简体   繁体   中英

Doing a JOIN in LINQ in C#

I have the following C# code

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. Essentially, I want a new User object that will look like this:

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. I do not know how to do a join with the roles variable. Is there a way to do this with LINQ? If so, how?

Use 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. 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".

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. 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
               });

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