我有以下权利关系。我想通过传递Project Id来过滤资源。
项目类
public class Project : EntityBase
{
public Project()
{
this.Tasks = new HashSet<Task>();
}
[Key]
public Guid GUID { get; set; }
public virtual ICollection<Task> Tasks { get; set; }
}
任务类
public class Task : IIdentifier
{
public Task()
{
this.Assignments = new HashSet<Assignment>();
}
[Key]
public Guid GUID { get; set; }
[ForeignKey("Projects")]
public Guid? ProjectId { get; set; }
public virtual Project Projects { get; set; }
public virtual ICollection<Assignment> Assignments { get; set; }
}
资源类
public class Resource : IIdentifier
{
public Resource()
{
this.Assignments = new HashSet<Assignment>();
}
[Key]
public Guid GUID { get; set; }
public virtual ICollection<Assignment> Assignments { get; set; }
}
作业类
public class Assignment : IIdentifier
{
[Key]
public Guid GUID { get; set; }
[ForeignKey("Tasks")]
public Guid TaskId { get; set; }
public virtual Task Tasks { get; set; }
[ForeignKey("Resources")]
public Guid ResourceId { get; set; }
public virtual Resource Resources { get; set; }
}
现在,我希望在传递Project GUID时获得所有资源
public IEnumerable<Resource> GetResourcesForViewsByProjectId(Guid ProjectId)
{
var x = from t in Uow.Tasks.GetAll().Where(con => con.ProjectId == ProjectId)
from a in Uow.Assignments.GetAll().Where(c => c.TaskId == t.GUID)
from r in Uow.Resources.GetAll()
.Where(r => r.Assignments.Where(con => con.ResourceId == r.GUID))
.DefaultIfEmpty()
select r;
return x;
}
但这不起作用。有什么建议吗?
答案 0 :(得分:1)
您应该可以使用Join方法。 试试这个:
public IEnumerable<Resource> GetResourcesForViewsByProjectId(Guid ProjectId)
{
var resources = Uow.Tasks.Join(
Uow.Assignments,
task => task.GUID,
assignment => assignment.TaskId,
(task, assignment) => new
{
Task = task,
Assignment = assignment
})
.Join(Uow.Resources,
j => j.Assignment.ResourceId,
resource => resource.GUID,
(j, resource) => new
{
Task = j.Task,
Assignment = j.Assignment,
Resource = resource
})
.Where(j => j.Task.ProjectId == ProjectId)
.Select(j => j.Resource);
return resources;
}
作为参考,LINQ Join功能在此处描述:https://msdn.microsoft.com/en-us/library/bb669071(v=vs.110).aspx