lambda 表达式尝试根据另一个列表查询一个列表

发布于 2024-10-09 08:52:29 字数 1357 浏览 2 评论 0原文

关于如何使用 lambda 表达式很好地完成此操作的一些建议。

我想要的是获得基于代理机构的展示位置列表。我想要其中placement.agency!=代理机构但其中placement.agencypersonnel包含任何代理机构工作人员的展示位置。

因此,如果安置不是针对该机构的,但该机构的工作人员参与了另一个机构的安置,

我不知道如何根据第二个条件进行查询。

所以类似:

// agency is being passed in
     var agencySupervisors = agency.AgencyPersonnel;

     return agency.Placements
            .Where(p => p.Supervisors.Contains(agencySupervisors))
            .Where(p => p.Agency != agency);

我知道 Contains 应该引用单个对象而不是集合 - 这就是它出错的原因..但我不确定如何让它检查集合中的所有对象。

也尝试过任何

 return agency.Placements
           .Where(p => agencySupervisors.Any<PlacementSupervisor>(p.Supervisors))
           .Where(p => p.Agency != agency);

所以希望这只是我使用错误的!

工作中的另一个扳手正在试图弄清楚安置主管和机构人事实体如何相互关联。我认为它与 AgencyPersonnelId = SupervisorId 相关联,所以我猜这也必须考虑到我的表达中。

谢谢!

编辑:如果两个列表中的对象类型不相同,我该如何处理 - 但我知道 ID 会匹配。我是否必须编写一个比较器并以某种方式将其合并到表达式中? IE。 AgencyPersonnelId = SupervisorId

我已经尝试过:

return placements
                .Where(p => p.Supervisors.Any(supervisor => agencySupervisors.Any(ap => ap.AgencyPersonnelId == supervisor.SupervisorId)));

但它没有给我任何结果,所以它显然是错误的。

编辑:实际上,当我尝试迭代返回集合中的位置时,我收到了空引用异常 - 所以我不确定它是否与我的表达式或我返回结果的方式有关.

After some advice on how to do this nicely using lambda expressions.

What I want is to get a list of placements based on an agency. I want the placements where the placement.agency != agency but where placement.agencypersonnel contains any of the agency staff.

So where the placement is not for that agency, but there are staff from that agency involved in another agency's placement

I don't know how to query based on the second condition.

So something like:

// agency is being passed in
     var agencySupervisors = agency.AgencyPersonnel;

     return agency.Placements
            .Where(p => p.Supervisors.Contains(agencySupervisors))
            .Where(p => p.Agency != agency);

I get that Contains is supposed to refer to a single object rather than a collection - which is why its erroring.. but I'm not sure how to get it to check against all objects in the collection.

Have also tried Any

 return agency.Placements
           .Where(p => agencySupervisors.Any<PlacementSupervisor>(p.Supervisors))
           .Where(p => p.Agency != agency);

So hopefully its just I'm using the wrong one!!

Another spanner in the works is trying to figure out how the placement supervisor and the agency personnel entities relate to one another.. I think its linked on AgencyPersonnelId = SupervisorId so I'm guessing that will also have to be factored into my expression.

Thanks!

Edit: How do I handle if the type of objects in the two list aren't the same - but I know that the Id will match. Do I have to write a comparer and somehow incorporate that into the expression?? ie. AgencyPersonnelId = SupervisorId

I have tried:

return placements
                .Where(p => p.Supervisors.Any(supervisor => agencySupervisors.Any(ap => ap.AgencyPersonnelId == supervisor.SupervisorId)));

But it is giving me no results so it is obviously wrong.

Edit: Actually when I try to iterate through the placements in the returned collection I'm getting a null reference exception - so I'm not sure if its something to do with my expression or the way I'm returning the results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她说她爱他 2024-10-16 08:52:29

您已接近 Any & 包含 - 同时尝试两者

return agency.Placements        
    .Where(p => agencySupervisors.Any(supervisor => p.Supervisors.Contains(supervisor))
    .Where(p => p.Agency != agency);

You are close with Any & Contains - try both at once

return agency.Placements        
    .Where(p => agencySupervisors.Any(supervisor => p.Supervisors.Contains(supervisor))
    .Where(p => p.Agency != agency);
花间憩 2024-10-16 08:52:29

我认为你也可以使用 .Intersect 来做到这一点:

return agency.Placements
         .Where(p => agencySupervisors.Intersect(p.Supervisors).Any()
                     && p.Agency != agency); 

I think you can do it with .Intersect also:

return agency.Placements
         .Where(p => agencySupervisors.Intersect(p.Supervisors).Any()
                     && p.Agency != agency); 
究竟谁懂我的在乎 2024-10-16 08:52:29

感谢大家的帮助 - 因为对象的类型不同,所以我最终不得不做一些不同的事情 - 但后来发现我能够使用他们的 ID 进行比较,所以结果是:

var agencySupervisors = (from ap in agency.AgencyPersonnel
                                    where ap != null
                                        select ap.AgencyPersonnelId).ToList();

            return 
                (from p in m_PlacementRepository.Linq orderby p.PlacementId select p)
                .Where(p => p.Agency != agency)
                .Where(p => p.Supervisors != null && p.Supervisors.Any(s => agencySupervisors.Contains(s.SupervisorId)));

另外,正如 Mikael 正确指出的那样,我是首先从错误的集合开始:)

Thanks everyone for the help - Because the objects were of different types I ended up having to do something a little different - but then found I was able to use their Ids for the comparison so the result was:

var agencySupervisors = (from ap in agency.AgencyPersonnel
                                    where ap != null
                                        select ap.AgencyPersonnelId).ToList();

            return 
                (from p in m_PlacementRepository.Linq orderby p.PlacementId select p)
                .Where(p => p.Agency != agency)
                .Where(p => p.Supervisors != null && p.Supervisors.Any(s => agencySupervisors.Contains(s.SupervisorId)));

Plus as Mikael rightly pointed out I was starting with the wrong collection in the first place :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文