Alex Polkhovsky
Alex Polkhovsky

Reputation: 3360

Entity Framework query using navigation properties

Users have many Roles. Roles have many Privileges. I need to get a list of privileges assigned to user based on roles user may have. I have something like this in mind but it gives me an IEnumerable of IEnumerable of Privilege:

List<Privilege> privileges = user.Roles.Select(r=>r.Privileges.Where(p=>p.IsActive));

How can I do this using navigation properties?

Upvotes: 0

Views: 221

Answers (2)

Bishnu Paudel
Bishnu Paudel

Reputation: 2079

Try this:

user.Roles.SelectMany(r => r.Privileges).Where(p=>p.IsActive).ToList();

Upvotes: 1

Michael Christensen
Michael Christensen

Reputation: 1786

SelectMany

It flattens the ienumerables as you go

Upvotes: 2

Related Questions