Created
January 30, 2015 21:57
-
-
Save ronnieoverby/496bec1957b61adb4acc to your computer and use it in GitHub Desktop.
Demonstrates efficiently querying the default MVC 5 entity framework model for users in a given role.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using WebApplication9.Models; | |
namespace WebApplication9 | |
{ | |
public class DemoGettingUsersInSpecificRole | |
{ | |
private readonly ApplicationDbContext _db; | |
public DemoGettingUsersInSpecificRole(ApplicationDbContext dbContext) | |
{ | |
if (dbContext == null) throw new ArgumentNullException("dbContext"); | |
_db = dbContext; | |
} | |
public IList<ApplicationUser> GetUsersInRole(string roleName) | |
{ | |
var query = | |
from r in _db.Roles | |
where r.Name == roleName | |
from ur in r.Users | |
join u in _db.Users on ur.UserId equals u.Id | |
select u; | |
var users = query.ToList(); | |
return users; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment