FluentNHibernate;如何使用QueryOver对子级别对象进行过滤?

发布于 2024-11-30 08:33:59 字数 915 浏览 1 评论 0原文

我试图了解如何检索对象图,我想在子级别对象上放置过滤器。所以考虑下面的课程。我希望查询检索 A 的完整集合及其 B 的完整集合,但对于 C 的集合,我只想根据“时间”属性检索特定日期范围的元素。

我试图理解这些材料: http://nhforge .org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx#Associations

也许这应该是显而易见的,但我很难了解如何使用第三级限制的扩展。所以真的很感激一些帮助..

 Class A
   public List<B> Bs;

 Class B
   public List<C> Cs;

 Class C
   public DateTime time;

我到目前为止的尝试:但它给出了例外;

IQueryOver<A, B> q = session.QueryOver<A>()
    .JoinQueryOver<B>(a => a.Bs).JoinQueryOver<C>(b => b.Cs)
    .Where(e => e.time.Date == System.DateTime.UtcNow.Date);

无法转换类型为“NHibernate.Criterion.QueryOver2[A,C]”的对象 输入“NHibernate.IQueryOver2[A,B]”。

I'm trying to understand how to retrieve an object graph, where I want to put a filter on a sub-level object. So considering below classes. I want the query to retrieve the full collection of A, with their full collections of B, but for the collections of C, I want only to retrieve element of a certain range of dates, based on the "time" property.

I'm trying to understand this material:
http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx#Associations

Maybe it should be obvious, but I have a hard time seeing how to use the extensions for this 3rd level restriction. So would really appreciate some help..

 Class A
   public List<B> Bs;

 Class B
   public List<C> Cs;

 Class C
   public DateTime time;

My attempt so far: But it gives exception;

IQueryOver<A, B> q = session.QueryOver<A>()
    .JoinQueryOver<B>(a => a.Bs).JoinQueryOver<C>(b => b.Cs)
    .Where(e => e.time.Date == System.DateTime.UtcNow.Date);

Unable to cast object of type 'NHibernate.Criterion.QueryOver2[A,C]'
to type 'NHibernate.IQueryOver
2[A,B]'.

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

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

发布评论

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

评论(2

╰ゝ天使的微笑 2024-12-07 08:33:59

假设您想在某个时刻将结果转换为列表,我会使用:

session.QueryOver<A>()
.JoinQueryOver<B>(a => a.Bs)
.Fetch(a => a.Bs).Eager
.JoinQueryOver<C>(b => b.Cs)
.Where(e => e.time.Date == System.DateTime.UtcNow.Date).List<A>

关键点是 JoinQueryOver 仅用于构建 where 子句。
要通过预加载包含集合,您需要使用 Fetch。

Assuming that you want to cast the results to a list at some point, I would use:

session.QueryOver<A>()
.JoinQueryOver<B>(a => a.Bs)
.Fetch(a => a.Bs).Eager
.JoinQueryOver<C>(b => b.Cs)
.Where(e => e.time.Date == System.DateTime.UtcNow.Date).List<A>

The key bit is that the JoinQueryOver is only for building the where clause.
To include collections via eager loading, you need to use Fetch.

ま昔日黯然 2024-12-07 08:33:59

如果上面的答案给你重复的答案,那是因为它是一个经典的 select n+1 问题。请参阅http://ayende.com/blog/4367/ eagerly-loading-entity-associations-efficiently-with-nhibernate

是一种有效加载这些集合而不会出现重复问题的方法。

if the above answer gives you duplicates it is because its an classic select n+1 problem. See http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate

for a way to efficiently load those collection without the duplication problem.

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