User
has many Roles
Task
has many Receivers
( type of Role
) User -> Role
Task -> Role
How can I write a query to get User tasks? I tried code below, but it returns an IEnumerable<ICollection>
;
v var userTasks = from role in Context.Users.Find(userId).Roles
join taskRole in Context.Tasks.SelectMany(t => t.Receivers) on role.Id equals taskRole.Id
select taskRole.Tasks;
In your SelectMany, store the corresponding task
Context.Users.Find(userId).Roles
.Join(Context.Tasks.SelectMany(t => t.Receivers.Select(tr => new {t, tr})),
r => r.Id,
t => t.tr.Id,
(r, t) => t.t)
.Distinct() // if task override Equals/HashCode
Use the double from clause instead of the join:
var userTasks = from role in Context.Users.Find(userId).Roles
from task in role.Tasks
select task;
Or if you prefer the short way:
var userTasks = Context.UserAccount.Find(userID).Roles.SelectMany(role => role.Tasks);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.