正在检查孙子记录到 return grand-parent。林克

Checking grandchildren records to return grand-parent. Linq

我有不同的角色,每个用户可以有多个角色。每个角色都以不同的方式连接到客户记录,例如业务分析师与项目有 many-to-many 关系,每个客户都有很多项目;而客户记录只能关联一个项目经理。

public class Customer
{
    public CustomerProjectManager ProjectManager { get; set; }
    public ICollection<Project> Projects{ get; set; }
    ...
}

public class Project 
{
    public ICollection<ProjectBusinessAnalyst> BusinessAnalysts { get; set; }
    public ICollection<ProjectDeveloper> ProjectDevelopers { get; set; }
    ...
}

public class ProjectDeveloper
{
    public int Id { get; set; }
    public Project Project{ get; set; }
    public int ProjectId { get; set; }
    public string DeveloperId { get; set; }
    public string DeveloperEmail { get; set; }
    public string DeveloperName { get; set; }
}

public class CustomerProjectManager
{
     public int Id { get; set; }
     public ICollection<Customer> Customers { get; set; }
     public string ProjectManagerId { get; set; }
     public string ProjectManagerEmail { get; set; }
     public string ProjectManagerName { get; set; }

     public CustomerProjectManager()
     {
         Customers = new List<Customer>();
     }
}

我需要根据角色获取客户记录。为了进一步解释,我需要组合根据分配给单个用户的不同角色获取的多个客户列表。我无法形成正确的 linq 查询。

我有一个示例查询,如下所述,有时 returns 正确的记录但是如果我有一个新用户并且没有客户分配给该用户,则查询 returns 所有现有客户.对我来说重要的是所有的组合和过滤都是在 Iqueryable

中完成的

请帮忙!

public async Task<List<Customer>> FetchCustomers(string userId, List<string> userRoles, string userEmail)
{
    if (userRoles.Contains("Admin"))
    {
         customer = _context.Customers;
    }
    else if (userRoles.Contains("Project Manager") ||
             userRoles.Contains("Business Analyst") ||
             userRoles.Contains("Developer"))
    {
         if (userRoles.Contains("Project Manager"))
         {
             customers = customers.Where(c => c.ProjectManager.ProjectManagerId == userId
                       || c.Projects.Any(op =>                                              
                          op.ProjectsCompleted.Any(assignee =>                                                           
                          assignee.UserId == userId)));
         }
         if (userRoles.Contains("Business Analyst"))
         {
             var allPossibleCustomers = _context.Customers.Where(c =>
                            c.Projects.Any(op => op.BusinessAnalysts.Any(ba => ba.BusinessAnalystId == userId)));

             customers = customers?.Union(allPossibleCustomers) ?? allPossibleCustomers;
         }
         if (userRoles.Contains(Roles.Developer.GetDescription()))
         {
              var allPossibleCustomers = _context.Customers.Where(c =>
              c.Projects.Any(op => op.PREDevDevelopersAssigned.Any(ba => ba.DeveloperId == userId)));
                    
             customers = customers?.Union(allPossibleCustomers) ?? allPossibleCustomers;
         }
    }
    var listData = await PagingList<Customer>.CreatePageAsync(customers, page, limit);
    return listData;
}

显然我试图 return 错误的列表。 linq查询正确。