使用Linq查询进行预先加载,但对详细信息有限制 [英] Eager loading with Linq query with restriction on details

查看:53
本文介绍了使用Linq查询进行预先加载,但对详细信息有限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何使用NHibernate的内置linq提供程序编写查询,包括渴望加载和详细信息限制?例如

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;

非常感谢.

卡斯滕

推荐答案

您可能要考虑在此处使用queryOver:-

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);

我可能是错的,但我认为NH LINQ提供程序目前无法支持.

I may be wrong but I don't think the NH LINQ provider can support this at the moment.

还请注意.left这很重要,请参阅此博客文章,以获取原因

Also note the .left this is important, see this blog post for reasons why

这篇关于使用Linq查询进行预先加载,但对详细信息有限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆