2016-03-31 16 views
1

Şu an aşağıdaki gibi bir yöntem kullanıyorum: db.GetProjectsAllowed(profileId, profOrgList, projectList). Bunu IDbSet<Project>'u kullanmak için dönüştürmek istiyorum, ancak ikinci LINQ sorgusunu nasıl alacağımı bilmiyorum.Bu uzantı yöntemini, DbContext yerine IDbSet <> kullanmak için kullanabilir miyim?

public static IQueryable<Project> GetProjectsAllowed 
(
    this IMkpContext db, 
    Guid profileId, 
    List<Guid> profOrgIds = null, 
    List<Guid> projectIds = null 
) 
{ 
    var projects = 
     (
      from p in db.Project 
       .Include(p => p.Proposals) 
       .Include(p => p.RoleAssignments) 
       .Include("RoleAssignments.AssigneeSnapshot") 
      where p.IsActive 
      select p); 

    if (profOrgIds != null && profOrgIds.Any()) 
    { 
     var profileIds = db.ProfileOrganization 
      .Where(po => po.IsActive && profOrgIds.Contains(po.OrganizationId)) 
      .Select(po => po.ProfileId); 
     projects = projects.Where(p => profileIds.Contains(p.CreatedById)); 
    } 

    if (projectIds != null && projectIds.Any()) 
     projects = projects.Where(proj => projectIds.Contains(proj.ProjectId)); 

    return projects;//.ToList(); 
} 

Bu IDbSet<Project> ya da değil kullanmak dönüştürebilir miyim?

+0

yapabildin ama yapacağınız ProfileOrganizasyon p geçmek zorunda yönteme bir parametre olarak rocount. – user2697817

cevap

1

Burada, neden bunu iki uzantı yöntemine bölmezsiniz? Bu, GetProjectsAllowed uzantı yöntemini daha fazla cohesive ve single responsible yapar.

İlk:

public static IEnumerable<Guid> GetProfileIds(
    this IDbSet<ProfileOrganization> profileOrganizations, 
    IEnumerable<Guid> profOrgIds = null) 
{ 
    return profOrgIds == null ? null : 
     from po in profileOrganizations 
     where po.IsActive 
     where profOrgIds.Contains(po.OrganizationId) 
     select po.OrganizationId; 
} 

Ve ikinci:

public static IQueryable<Project> GetProjectsAllowed(
    this IDbSet<Project> projects, 
    IEnumerable<Guid> profileIds, 
    IEnumerable<Guid> projectIds = null) 
{ 
    var activeProjects = 
     from project in projects 
     //.Include(.. 
     where project.IsActive 
     select project; 

    if (profileIds != null && profileIds.Any()) 
    { 
     activeProjects = activeProjects.Where(p => profileIds.Contains(p.CreatedById)); 
    } 

    if (projectIds != null && projectIds.Any()) 
    { 
     activeProjects = activeProjects.Where(proj => projectIds.Contains(proj.ProjectId)); 
    } 

    return activeProjects;//.ToList(); 
} 

Ve sonra tüketici bunu böyle arayabilirsiniz:

var profileIds = db.ProfileOrganization.GetProfileIds(profOrgIds); 
var projectsAllowed = db.Projects.GetProjectsAllowed(profileIds, projectIds); 
Tabii
İlgili konular