简体   繁体   English

实体框架过滤器子集合

[英]entity framework filter child collection

I have these classes: 我有这些课:

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; }
    }

and I need to take all projects belong to each user based on tasks assigned to him/her I have come up with this solution but I could not move forward: 并且我需要根据分配给他/她的任务来考虑属于每个用户的所有项目,我已经提出了此解决方案,但是我无法前进:

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;
        }

I do not know how to correlate and filter tasks for each project by taskId. 我不知道如何通过taskId为每个项目关联和过滤任务。

 context.Projects.Where( 
            p => p.Tasks.Any( 
                        t => t.TaskAssigns.Any( ta => ta.UserId == userId ) 
                        ) 
                      )

When you are doing this type of task you want to correlate the data to who owns what. 当您执行此类任务时,您想要将数据与谁拥有什么相关联。

So a project has tasks. 因此,一个项目有任务。 A user has tasks assigned to them. 用户已分配了任务。 A user is on a project. 用户在一个项目上。

So for a given user I want their projects. 因此,对于给定的用户,我想要他们的项目。 Right? 对? (you are nodding your head now) But initially I only know the tasks from a users point of view and the tasks have knowledge of the project the user is on. (您现在正在点头)但最初,我仅从用户角度了解任务,并且这些任务具有用户所在项目的知识。

   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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM