我有这些课程:
public class Project
{
public int ProjectId { get; set; }
public int AccountId { get; set; }
public int UserId { get; set; }
public string Name { get; set; }
public DateTime StartDate { get; set; }
public string Description { get; set; }
public string Tags { get; set; }
public virtual List<Task> Tasks { get; set; }
}
public class Task
{
public int TaskId { get; set; }
public int ProjectId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime? DueDate { get; set; }
public int TaskOwnerId { get; set; }
public string Tags { get; set; }
public virtual Project Project { get; set; }
public virtual List<TaskAssigned> TaskAssigns { get; set; }
public virtual List<TaskComment> TaskComments { get; set; }
}
public class TaskAssigned
{
public int TaskAssignedId { get; set; }
public int TaskId { get; set; }
public int UserId { get; set; }
}
我需要根据分配给他/她的任务来获取属于每个用户的所有项目 我已经提出了这个解决方案,但我无法前进:
public List<Project> GetProjectsByAccountIdUserId(int accountId, int userId)
{
var tasks = context.TaskAssigns.Where(ta => ta.UserId == userId).ToList();
var projects =context.Projects.Where(p => p.AccountId == accountId).Include("Tasks").ToList();
return projects;
}
我不知道如何通过taskId关联和过滤每个项目的任务。
答案 0 :(得分:9)
context.Projects.Where(
p => p.Tasks.Any(
t => t.TaskAssigns.Any( ta => ta.UserId == userId )
)
)
答案 1 :(得分:0)
当您执行此类任务时,您希望将数据与谁拥有什么相关联。
所以项目有任务。用户已分配任务。用户正在进行项目。
因此,对于给定的用户,我想要他们的项目。对? (你现在正在点头)但最初我只从用户的角度知道任务,而且任务知道用户所在的项目。
var projectIdentities = (from t in context.Tasks
join a in context.TaskAssigned on t.TaskId equals a.TaskId
where a.UserId == userId
select t.ProjectId).Distinct();
List<Project> userProjects = (from p in context.Projects
where projectIdentities.Contains(p.ProjectId)
select p).ToList();
return userProjects;