static void Main(string[] args) { DecoupageStoreDbContext db = new DecoupageStoreDbContext(); var l = db.Users .Select(u => new { Roles = u.Roles.Join(db.Roles, ur => ur.RoleId, r => r.Id, (ur, r) => new { Role = r.Name }) .ToList(), UserName = u.UserName }) .ToList(); foreach (var x in l) { Console.WriteLine(); Console.Write(x.UserName + " Roles: "); foreach (var role in x.Roles) { Console.Write(role.Role + " "); } } }
public IQueryable<UserNameRoleModel> GetUserNameRolesForPage(int page = GlobalConstants.defaultPageNumber, int usersPerPage = GlobalConstants.defaultItemsPerPage) { DecoupageStoreDbContext dbContext = new DecoupageStoreDbContext(); return dbContext.Users .Select(usr => new UserNameRoleModel { UserName = usr.UserName, Roles = usr.Roles .Join(dbContext.Roles, ur => ur.RoleId, r => r.Id, (ur, r) => r.Name) .ToList() }) .OrderBy(usr => usr.UserName) .Skip(page * usersPerPage) .Take(usersPerPage); }