Reputation: 2793
So, that's my model .. nice and complex.
I'm looking to get Areas by a UserID. If I were doing this in SQL, I would do a bunch of joins all the way up to the Users table. How would you do this in LINQ query syntax or method chaining?
I can do this pretty straightforward from the other way around, but then I have to do a lot of extra work to flatten the resulting graph and it also requires pulling back all the entities in between.
If I can optionally include AreasPermissions & Permissions, that would be gravy .. but at this point I wouldn't mind an additional query to fetch those.
Another option I was floating was using a function import to a sproc and map that to an Area .. but when I start needing to include other entities it makes that option less elegant. I'm also trying to avoid using sprocs just to use sprocs because it's always a slippery slope with folks .. 'use a sproc for one thing' tends to mold into 'don't use EF (table access) for anything'.
var userByID = new Func<User, bool>(x => x.UserId.Equals(userID, StringComparison.InvariantCultureIgnoreCase));
var user = this._context.Users
.Include("TeamsUsers.Team.TeamsRoles.Role.RolesAreasPermissions.AreaPermission.Area")
.Include("TeamsUsers.Team.TeamsRoles.Role.RolesAreasPermissions.AreaPermission.Permission")
.Single(userByID);
Upvotes: 1
Views: 222
Reputation: 11403
I have no way of testing this, but I think it should work:
var result =
from user in _context.Users
where user.Id == userId
from teamUser in user.TeamUsers
from teamRole in teamUser.Team.TeamRoles
from roleAreaPermission in teamRole.Role.RoleAreaPermissions
select roleAreaPermission.AreaPermission.Area;
Upvotes: 4