Linq to entity可以选择包含吗?

时间:2015-12-10 19:00:14

标签: c# linq-to-entities

我不确定我是否可以这样做,但如果可以的话,我不确定最佳方法。

我有一个名为RoleAssignment的模型/表,其外键ID可能是两个表中的一个(Profile或ProfileSnapshot)。我想获得一个包含任何一个的可列举的RoleAssignment。当我执行我的linq查询,并尝试包括两个表时,我没有记录。我相信这是因为没有记录会加入这两个表格。

稍后,我会做if (item.Assignee == null) do something with item.AssigneeSnapshot

之类的事情

我以为我可以使用Default if empty执行linq查询,但不确定语法是否正确。

这是我的模特:

public class RoleAssignment : BaseEntity
{
    [Key]
    public int RoleAssignmentId { get; set; }

    public Guid AssigneeId { get; set; }

    [ForeignKey("AssigneeId")]
    public virtual Profile Assignee { get; set; }

    [ForeignKey("AssigneeId")]
    public virtual ProfileSnapshot AssigneeSnapshot { get; set; }
}

这就是我正在尝试使用Linq:

[HttpPost, ActionName("_roleassignmentindex")]
[ValidateAntiForgeryToken]
public virtual ActionResult _RoleAssignmentIndex(RoleAssignmentFilterViewModel filter)
{
    var rolesAssmnt = db.RoleAssignment
        .Include(r => r.Assignee)
        .Include(r => r.AssigneeSnapshot)
        .Include(r => r.OrganizationAssignedTo)
        .Where(r => r.IsActive);

    if (filter.ProfileToSearch != null && filter.ProfileToSearch != Guid.Empty)
        rolesAssmnt = rolesAssmnt.Where(r => r.AssigneeId == filter.ProfileToSearch);

    if (filter.RoleToSearch != null && filter.RoleToSearch > 0)
        rolesAssmnt = rolesAssmnt.Where(r => r.RoleId == filter.RoleToSearch);

    return View(rolesAssmnt.ToList());
}

2 个答案:

答案 0 :(得分:2)

你需要像这样分解你的查询:

[HttpPost, ActionName("_roleassignmentindex")]
[ValidateAntiForgeryToken]
public virtual ActionResult _RoleAssignmentIndex(RoleAssignmentFilterViewModel filter)
{
    List<RoleAssignment> = null;

    if (filter.ProfileToSearch != null && filter.ProfileToSearch != Guid.Empty)
    {
       rolesAssmnt = db.RoleAssignment
        .Include(r => r.Assignee)
        .Include(r => r.AssigneeSnapshot)
        .Where(r => r.IsActive && r.AssigneeId == filter.ProfileToSearch)
        .ToList();

       rolesAssmnt.AddRange(db.RoleAssignment
        .Include(r => r.Assignee)
        .Include(r => r.OrganizationAssignedTo)
        .Where(r => r.IsActive && r.AssigneeId == filter.ProfileToSearch));            
    }

    else if (filter.RoleToSearch != null && filter.RoleToSearch > 0)
    {
       rolesAssmnt = db.RoleAssignment
        .Include(r => r.Assignee)
        .Include(r => r.AssigneeSnapshot)
        .Where(r => r.IsActive && r.RoleId == filter.RoleToSearch)
        .ToList();

       rolesAssmnt.AddRange(db.RoleAssignment
        .Include(r => r.Assignee)
        .Include(r => r.OrganizationAssignedTo)
        .Where(r => r.IsActive && r.RoleId == filter.RoleToSearch));
    }

    return View(rolesAssmnt);
}

答案 1 :(得分:0)

结束这样做,但感谢The Sharp Ninja:

[HttpPost, ActionName("_roleassignmentindex")]
[ValidateAntiForgeryToken]
public virtual ActionResult _RoleAssignmentIndex(RoleAssignmentFilterViewModel filter)
{
    List<RoleAssignment> rolesAssmnt = null;

    if (filter.ProfileToSearch != null && filter.ProfileToSearch != Guid.Empty)
    {
        rolesAssmnt = db.RoleAssignment
            .Include(r => r.Assignee)
            .Include(r => r.OrganizationAssignedTo)
            .Where(r => r.IsActive &&
                r.AssigneeId == filter.ProfileToSearch
            ).ToList();

        rolesAssmnt.AddRange(db.RoleAssignment
            .Include(r => r.AssigneeSnapshot)
            .Where(r => r.IsActive &&
                r.AssigneeSnapshot.ProfileId == filter.ProfileToSearch
            ).ToList());
    }
    else
    {
        rolesAssmnt = db.RoleAssignment
            .Include(r => r.Assignee)
            .Include(r => r.OrganizationAssignedTo)
            .Where(r => r.IsActive
            ).ToList();

        rolesAssmnt.AddRange(db.RoleAssignment
            .Include(r => r.AssigneeSnapshot)
            .Where(r => r.IsActive
            ).ToList());
    }

    if (filter.RoleToSearch != null && filter.RoleToSearch > 0)
        rolesAssmnt = rolesAssmnt.Where(r => r.RoleId == filter.RoleToSearch).ToList();

    return View(rolesAssmnt);
}