NHibernate 3.1 Linq 与 Contains 和 Any

发布于 2024-11-07 13:25:52 字数 1308 浏览 0 评论 0原文

我们正在升级到 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.Query() 和 person 在 IQueryable.Query() 中。
本质上,我试图将注册限制在 person1p 的显式管理器。我可以通过连接来做到这一点,但不能通过 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 技术交流群。

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

发布评论

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

评论(2

缱倦旧时光 2024-11-14 13:25:52

虽然 Contains 似乎无法正常工作,但使用 Any 可以:

var results = from r in registrations 
              where ( 
                         from p in persons 
                         where p.ExplicitManagers.Any(m => m.Manager == manager) 
                         select p 
                     ).Any(p=>p == r.Registrant) 
              select r; 

Whilst Contains doesn't seem to work properly, using Any does:

var results = from r in registrations 
              where ( 
                         from p in persons 
                         where p.ExplicitManagers.Any(m => m.Manager == manager) 
                         select p 
                     ).Any(p=>p == r.Registrant) 
              select r; 
请爱~陌生人 2024-11-14 13:25:52

您可以毫无问题地自行执行子查询吗?

var result = from p in persons 
where p.ExplicitManagers.Any(m => m.Manager == manager) 
select p;

Can you execute the sub query on its own without problems?

var result = from p in persons 
where p.ExplicitManagers.Any(m => m.Manager == manager) 
select p;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文