Eager loading with Linq query with restriction on details

 ̄綄美尐妖づ 提交于 2019-12-25 08:41:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!