Skip to content

Instantly share code, notes, and snippets.

@ronnieoverby
Created January 30, 2015 21:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronnieoverby/496bec1957b61adb4acc to your computer and use it in GitHub Desktop.
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.
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