Home > front end >  checking a field in lambda with a list of number
checking a field in lambda with a list of number

Time:04-24

I have 3 tables with many to many relation

User Table
Id       Username

===================== 1 user1 2 user2

Role Table
Id       Rolename

===================== 1 Ad min 2 Superuser 3 Operator every user can have many Roles

 UserRoles Table

 Id   UserId   RoleId 
====================
 1     1         2
 2     1         1
 3     2         3

I want to have list of users who have Roles (ex: 1 and 2 )

  user = _context
    .Users
    .Include(m => m.UserRoles)
    .Where(p => p.Mobile.Equals(Username) && 
                p.IsActive == true && 
                p.UserInRoles.Any(c => c.RoleId == 1 || c.RoleId == 2))
    .ToList();

but I have set the Roles Id manually, what if I want pass the Roles Id as a list and check it with that list like

 List<long> roleids =new List<long> { 1, 2 };

pass the roleids

how should I change the lambda?

CodePudding user response:

 user = _context
.Users
.Include(m => m.UserRoles)
.Where(p => p.Mobile.Equals(Username) && 
            p.IsActive == true && 
            p.UserInRoles.Any(c => roleids.Contains(c)))
.ToList();
  • Related