根据满足的较低级别标准获取顶级实体

时间:2013-09-03 22:17:18

标签: c# entity-framework lambda

我有以下数据库设置。我希望实现的是在Worker tabe中搜索一个worker,并返回所有BusinessAreas,结果用户将根据该图进行调整。我希望在Lambda表达式中完成此操作并返回顶级BusinessArea实体。如果需要的话,我可以让工作人员通过一些流程重新达到我想要的水平,但这需要我迭代不同的水平,直到我达到顶级水平。

enter image description here

1 个答案:

答案 0 :(得分:2)

听起来你想要这样的东西,为单个用户查找业务领域:

var results = WFE.BusinessAreas
    .Where(ba => ba.Processes
        .Any(p => p.WorkerProcesses
            .Any(wp => wp.UserId == "pmarshall")));

或者在查询语法中:

var results =
    from ba in WFE.BusinessAreas
    where ba.Processes.Any(p => p.WorkerProcesses.Any(wp => wp.UserId == "pmarshall"))
    select ba;

或者:

var results =
    from ba in WFE.BusinessAreas
    from p in ba.Processes
    from wp in p.WorkerProcesses
    where wp.UserId == "pmarshall"
    select ba;

如果您想为所有用户找到所有区域,您需要执行以下操作:

var results =
    (from ba in WFE.BusinessAreas
     from p in ba.Processes
     from wp in p.WorkerProcesses
     select new { ba, wp.UserId })
    .Distinct()
    .ToLookup(x => x.UserId, x => x.ba);

然后,您可以使用results["pmarshall"]访问单个用户的结果。