如何使用NHibernate的内置linq提供程序编写查询,包括对细节的预先加载和限制?例如
public class Library
{
public Library()
{
Books = new List<Book>();
}
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Book> Books { get; protected internal set; }
}
public class Book
{
public Book()
{
Pages = new List<Page>();
}
public virtual int Id { get; set; }
public virtual Library Library { get; set; }
public virtual string Title { get; set; }
}
以下查询显示我需要的内容,但不会急切加载
var query = from master in session.Query<Library>()
from detail in master.Books
where detail.Title == detailValue
select master;
以下查询不起作用...
var query = from master in session.Query<Library>()
// not allowed - causes Runtime error
.FetchMany(m => m.Details.Where(d => d.Value == detailValue))
select master;
提前多多感谢。
的Carsten
答案 0 :(得分:1)
您可能需要考虑在此处使用queryOver: -
Book book = null;
var query =
Session.QueryOver<Library>()
.Fetch(f => f.Books).Eager
.Left.JoinAlias(f => f.Books, () => book)
.Where(() => actor.book == detailValue);
我可能错了,但我认为NH LINQ提供商目前不支持这一点。
另请注意.left
这很重要,请参阅reasons why