lambda 表达式尝试根据另一个列表查询一个列表
关于如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已接近
Any
&包含
- 同时尝试两者You are close with
Any
&Contains
- try both at once我认为你也可以使用
.Intersect
来做到这一点:I think you can do it with
.Intersect
also:感谢大家的帮助 - 因为对象的类型不同,所以我最终不得不做一些不同的事情 - 但后来发现我能够使用他们的 ID 进行比较,所以结果是:
另外,正如 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:
Plus as Mikael rightly pointed out I was starting with the wrong collection in the first place :)