【发布时间】:2011-08-14 10:29:57
【问题描述】:
我发现了 2 个类似的问题:
- Multiple Fetches in linq to nhibernate
- Is this the right way of using ThenFetch() to load multiple collections?
根据this page:
注意不要急于获取 多个集合属性 同时。虽然这个说法 可以正常工作:
var employees = session.Query<Employee>() .Fetch(e => e.Subordinates) .Fetch(e => e.Orders).ToList();它执行笛卡尔积查询 针对数据库,所以总 返回的行数将是 总计 下属乘以总计 订单。
假设我有以下模型:
public class Person
{
public virtual int Id { get; private set; }
public virtual ICollection<Book> Books { get; set; }
public virtual ICollection<Article> Articles { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
使用 QueryOver/Linq 急切地加载所有人的书籍、文章和地址的最简单方法是什么(不返回笛卡尔积)?
谢谢
更新:
请参阅下面的cremor 的answer 和this thread 的Florian Lim 的answer。以下代码运行良好,只需往返数据库一次。
var persons = session.QueryOver<Person>()
.Future<Person>();
var persons2 = session.QueryOver<Person>()
.Fetch(x => x.Books).Eager
.Future<Person>();
var persons3 = session.QueryOver<Person>()
.Fetch(x => x.Articles).Eager
.Future<Person>();
var persons4 = session.QueryOver<Person>()
.Fetch(x => x.Addresses).Eager
.Future<Person>();
【问题讨论】:
标签: nhibernate linq-to-nhibernate queryover