FluentNHibernate;如何使用QueryOver对子级别对象进行过滤?
我试图了解如何检索对象图,我想在子级别对象上放置过滤器。所以考虑下面的课程。我希望查询检索 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.QueryOver
2[A,C]”的对象 输入“NHibernate.IQueryOver
2[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.QueryOver
2[A,C]'
2[A,B]'.
to type 'NHibernate.IQueryOver
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您想在某个时刻将结果转换为列表,我会使用:
关键点是 JoinQueryOver 仅用于构建 where 子句。
要通过预加载包含集合,您需要使用 Fetch。
Assuming that you want to cast the results to a list at some point, I would use:
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.
如果上面的答案给你重复的答案,那是因为它是一个经典的 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.