急切地获取多个集合属性(使用 QueryOver/Linq)?

发布于 2024-11-03 15:20:09 字数 2194 浏览 0 评论 0原文

我发现了 2 个类似的问题:

根据此页面 :

注意不要急于获取 多个集合属性 同一时间。虽然这个说法 会工作得很好:

var 员工 = session.Query<员工>()
    .Fetch(e => e.下属)
    .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回答下面和Florian Lim答案 此线程。以下代码运行良好,只需与数据库进行一次往返。

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

I found 2 similar questions:

According to this page:

Be careful not to eagerly fetch
multiple collection properties at the
same time. Although this statement
will work fine:

var employees = session.Query<Employee>()
    .Fetch(e => e.Subordinates)
    .Fetch(e => e.Orders).ToList();

It executes a Cartesian product query
against the database, so the total
number of rows returned will be the
total Subordinates times the total
orders.

Lets say I have the following model:

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

What is the simplest way to eagerly load all persons with their Books, Articles, and Addresses using QueryOver/Linq (without returning a Cartesian product)?

Thanks


Update:

See cremor's answer below and Florian Lim's answer in this thread. The following code works nicely, only one round-trip to the database.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回心转意 2024-11-10 15:20:09

如果可能的话,我更喜欢使用 linq 提供程序,特别是如果您使用较新版本的 nhibernate (>= 4.0)。只要您将集合映射为 ISet(需要 .net Framework >= 4),我们就可以将其转换为 ISet,这样我们就可以进行急切加载并避免笛卡尔积。我觉得这不是大肆宣传的东西,但我更喜欢这种适用于其他任何方法的方法:

public class Person
{
    public virtual int Id { get; private set; }
    public virtual ISet<Book> Books { get; set; }
    public virtual ISet<Article> Articles { get; set; }
    public virtual ISet<Address> Addresses { get; set; }
}

public Person()
{
    this.Books = new HashSet<Book>();
    this.Articles = new HashSet<Article>();
    this.Addresses = new HashSet<Address>();
}

如果您的集合像上面那样定义,那么您可以执行以下操作,并且仍然避免笛卡尔积问题:

var persons = session.Query<Person>()
                     .FetchMany(x => x.Books)
                     .FetchMany(x => x.Articles)
                     .FetchMany(x => x.Addresses)
                     .ToList();

I prefer to use the linq provider if at all possible especially if you are using newer versions of nhibernate (>= 4.0). As long as you have your collections mapped as ISets (requires .net framework >= 4) which we converted to such that we could do eager loading and avoid cartesian products. I feel like this isn't something that is heavily advertised but I prefer this method where applicable over anything else :

public class Person
{
    public virtual int Id { get; private set; }
    public virtual ISet<Book> Books { get; set; }
    public virtual ISet<Article> Articles { get; set; }
    public virtual ISet<Address> Addresses { get; set; }
}

public Person()
{
    this.Books = new HashSet<Book>();
    this.Articles = new HashSet<Article>();
    this.Addresses = new HashSet<Address>();
}

If you have your collections defined like the above then you can do the following and still avoid cartesian product issues:

var persons = session.Query<Person>()
                     .FetchMany(x => x.Books)
                     .FetchMany(x => x.Articles)
                     .FetchMany(x => x.Addresses)
                     .ToList();
流云如水 2024-11-10 15:20:09
public IList<Person> GetAll()
{
    var persons = session.QueryOver<Person>()
        .Future<Person>();

    session.QueryOver<Person>()
        .Fetch(x => x.Books).Eager
        .Future<Person>();

    session.QueryOver<Person>()
        .Fetch(x => x.Articles).Eager
        .Future<Person>();

    session.QueryOver<Person>()
        .Fetch(x => x.Addresses).Eager
        .Future<Person>();

    return persons.ToList();
}
public IList<Person> GetAll()
{
    var persons = session.QueryOver<Person>()
        .Future<Person>();

    session.QueryOver<Person>()
        .Fetch(x => x.Books).Eager
        .Future<Person>();

    session.QueryOver<Person>()
        .Fetch(x => x.Articles).Eager
        .Future<Person>();

    session.QueryOver<Person>()
        .Fetch(x => x.Addresses).Eager
        .Future<Person>();

    return persons.ToList();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文