0

我有这些课程:

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 关联和过滤每个项目的任务。

4

2 回答 2

9
 context.Projects.Where( 
            p => p.Tasks.Any( 
                        t => t.TaskAssigns.Any( ta => ta.UserId == userId ) 
                        ) 
                      )
于 2012-08-13T15:44:18.167 回答
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;
于 2012-08-13T15:43:49.777 回答