下面显示的查询非常简单,它只是为指定客户提取任务。我现在希望能够做的是获取传递给此函数的UserId并验证用户是否有权查看任务。
var dbData = await db.Tasks
.Where(a => a.CustomerId == customerId)
.OrderBy(a => a.CreatedDateTime).ToListAsync();
TasksId的Tasks表中有一个属性。用户可以通过UserOrganizations表属于n + 1个组织。采用已知UserId
并验证Task.OrganizationId的最佳方法之一是用户之一?
答案 0 :(得分:1)
如果关系不是Tasks
类的属性,则可以在query-syntax中编写连接。这些方面的东西:
var dbData = await (from t in db.Tasks
join uo in UserOrganizations on t.OrganizationId equals uo.OrganizationId
join u in Users on uo.UserId equals u.UserId
where t.CustomerId == customerId && u.UserId == theUserId
order by t.CreatedDateTime
select t).ToListAsync();
根据您生成的数据类的方式,您可能已经在Tasks
类上拥有导航属性,允许您这样做:
var dbData = await db.Tasks
.Where(a => a.CustomerId == customerId && a.Organization.UserOrganizations.Any(uo => uo.UserId == theUserId)
.OrderBy(a => a.CreatedDateTime).ToListAsync();
答案 1 :(得分:0)
var dbData = await db.Tasks
.Where(a => a.CustomerId == customerId
&& a.Organization.Users
.Any(u=>u.UserId == customerId)))
.OrderBy(a => a.CreatedDateTime).ToListAsync();
这给出了外键设置和关系可通过实体导航。