NHibernate 3.1 Linq 与 Contains 和 Any
我们正在升级到 NH3.1,进展顺利 - 据我们所知,现有代码一切正常。之一 从 2 迁移到 NH3 的动机是利用 Linq 支持,总体来说效果很好。然而我是 与一些更复杂的 where 子句作斗争,特别是当我 想要基于子集合进行检查:
var results = from r in registrations
where (
from p in persons
where p.ExplicitManagers.Any(m => m.Manager == manager)
select p
).Contains(r.Registrant)
select r;
模型所在位置:p
是一个 Person
,并且 registration
r
具有类型为 Person
的注册者< br> p
包含 ExplicitManager
关联实体的集合,这些实体 持有对另一个Person
(经理)的引用。
注意:registrations 是一个 IQueryable
和 person 在 IQueryable
中。
本质上,我试图将注册限制在 person1
是 p
的显式管理器。我可以通过连接来做到这一点,但不能通过 Contains
子查询。
我收到以下错误:
“系统.InvalidOperationException: 序列包含多个 匹配元素”
这样做作为子查询的原因是因为最终我需要 将检查管理器的逻辑外部化以使其可重用 (它实际上更复杂,但我在这个例子中简化了它 因为正是 Contains
中的 Any
导致了悲伤)。
当没有带有 Any 的子查询时,Contains
似乎工作正常。 这是我做错了什么,还是不受支持或 一个错误,还有其他方法可以实现同样的效果吗?
非常感谢您提供的任何帮助。
We are in the process of upgrading to NH3.1 which is going well -
everything is working as far as we can tell with existing code. One of
the motivations to move to NH3 from 2 has been to leverage the Linq
support and generally it is working really well. However I am
struggling with some more complex where clauses, specifically when I
want to check based on a sub-collection:
var results = from r in registrations
where (
from p in persons
where p.ExplicitManagers.Any(m => m.Manager == manager)
select p
).Contains(r.Registrant)
select r;
where the model is:p
is a Person
and a registration
r
has a registrant of type Person
p
contains a collection of ExplicitManager
associative entities which
hold a reference to another Person
(manager).
note: registrations is an IQueryable<Registration>.Query()
and persons
in an IQueryable<Person>.Query()
.
Essentially I am trying to restrict the registrations to where person1
is an explicit manager of p
. I can do this via joins but not through
the Contains
subquery.
I get the following error:
"System.InvalidOperationException :
Sequence contains more than one
matching element"
the reason for doing this as a sub-query is because ultimately I need
to externalize the logic for checking the managers to make it reusable
(it actually is more complex but I have simplified it for this example
because it is the Any
within a Contains
which is causing the grief).
Contains
seems to work fine when not having a sub-query with Any.
Is this something I am doing wrong, or is it something unsupported or
a bug, and is there another way of achieving the same thing?
Many thanks for any help you can give.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然
Contains
似乎无法正常工作,但使用Any
可以:Whilst
Contains
doesn't seem to work properly, usingAny
does:您可以毫无问题地自行执行子查询吗?
Can you execute the sub query on its own without problems?