问题
How can I write a query with the build-in linq provider of NHibernate including eager loading and restrictions on the details? For example
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; }
}
the following query shows what I need but does not load eagerly
var query = from master in session.Query<Library>()
from detail in master.Books
where detail.Title == detailValue
select master;
The following query does not work ...
var query = from master in session.Query<Library>()
// not allowed - causes Runtime error
.FetchMany(m => m.Details.Where(d => d.Value == detailValue))
select master;
Thanks a lot in advance.
Carsten
回答1:
You may want to consider using queryOver here instead:-
Book book = null;
var query =
Session.QueryOver<Library>()
.Fetch(f => f.Books).Eager
.Left.JoinAlias(f => f.Books, () => book)
.Where(() => actor.book == detailValue);
I may be wrong but I don't think the NH LINQ provider can support this at the moment.
Also note the .left
this is important, see this blog post for reasons why
来源:https://stackoverflow.com/questions/7432408/eager-loading-with-linq-query-with-restriction-on-details