ThenFetch中的多次提取

Fel*_*ano 5 nhibernate linq-to-nhibernate

我有一个关联实体,<many-to-one>并且该实体有两个<many-to-one>我想一次获取的实体.我可以通过此查询实现此目的:

 var tshead = session.Query<MainEntity>()
                .Fetch(r=>r.FirstAssoc).ThenFetch(p=>p.Other)
                .Fetch(r=>r.FirstAssoc).ThenFetch(p=>p.Another)
                .Take(10)
                .ToList();
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我必须写两次,.Fetch(r=>r.FirstAssoc) 我确信我可以避免这种情况,但我无法弄清楚如何.任何的想法 ?

谢谢 !

Phi*_*ill 3

如果您选择特定的“MainEntity”,那么您可以使用 QueryOver 来执行此操作,如下所示:

 FirstAssoc firstAssoc = null;
 Other other = null;
 Another another = null;

 tshead = session.QueryOver<MainEntity>()
               .Where(x => x.Id == id)
               .JoinAlias(x => x.FirstAssoc, () => firstAssoc)
               .JoinAlias(() => firstAssoc.Other, () => other)
               .JoinAlias(() => firstAssoc.Another, () => another)
               .SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)

我在这里写过:

http://www.philliphaydon.com/2011/04/nhibernate-querying-relationships-are-depth/

你不能这样做:

一对多多

只有一对多一

我不确定你可以做Many-Many-One,转换它太困难了。