【问题标题】:Linq query for filter object graph过滤器对象图的 Linq 查询
【发布时间】:2015-08-05 15:16:57
【问题描述】:

我有以下实体关系。我想通过传递项目 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; }
}

现在我想在传递项目 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;
    }

但这不起作用。有什么建议吗?

【问题讨论】:

    标签: c# linq join object-graph


    【解决方案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 连接功能:https://msdn.microsoft.com/en-us/library/bb669071(v=vs.110).aspx

    【讨论】:

    • 刚刚注意到您正在使用 GetAll() 方法 - 您可能需要在 Uow.Tasks、Uow.Assignments 和 Uow.Resources 的末尾添加该方法。如果是这种情况,我会更新我的示例
    猜你喜欢
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多