question

JohnZeen-0539 avatar image
0 Votes"
JohnZeen-0539 asked YijingSun-MSFT answered

LINQ using multiple role names while searching AD

Hello,

I need to ensure that I only get a user who is a part of one of the roles using linq but not sure how to provide a list of roles in the query. For example my requirement is to only get a user who is a part of either admin or custadmin role. Most of the examples I see have something like this for checking one role only:

var roleId = context.Roles.Where(m => m.Name == "admin").Select(m => m.Id).SingleOrDefault();


var users = context.Users
.Where(u => u.Roles.Any(r => r.RoleId == roleId)).ToList();

Thanks
John

dotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @JohnZeen-0539 ,
I think you could use the language specific OR condition.Just like this:

 var roleId = context.Roles.Where(m => m.Name == "admin"|| m.Name=="custadmin").Select(m => m.Id).SingleOrDefault();

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.