多次在linq中获取以休眠 [英] Multiple Fetches in linq to nhibernate

查看:61
本文介绍了多次在linq中获取以休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看

注意不要急于获取 处的多个集合属性 同时.虽然这句话 会正常工作:

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

var员工= session.Query() .Fetch(e => e.下属) .Fetch(e => e.Orders).ToList();

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

我需要获取2个引用,所以我需要做类似的事情.有没有更好的方法可以做到这一点.

I need to fetch 2 references so I would need to do something like that. Is there a better way of doing this.

我不能在子对象中执行.ThenFetchMany(),但是要在同一个级别上执行.

I can't do .ThenFetchMany() as it goes to the into the child objects but the ones I am after on the same level.

推荐答案

查询仍然会返回您想要的结果,但是如上所述,它将返回笛卡尔积,即SQL查询将返回count(e.Subordinates) * count(e.Orders)结果,这可能会很快加起来,尤其是如果您有两个以上的集合.

Well, the query will still return the results you want, but as stated, it will return a cartesian product, i.e. the SQL query will return count(e.Subordinates) * count(e.Orders) results, which could add up pretty quickly, especially if you have more than just two collections.

NHibernate在2.1版本中引入了未来.不幸的是,当前的NHibernate 3.0版本似乎无法使它们与NHibernate.Linq(session.Query<T>())一起使用.

NHibernate introduced Futures with the 2.1 release. Unfortunately there seems to be no way in the current NHibernate 3.0 release to make them work with NHibernate.Linq (session.Query<T>()).

将来,您可以在一次往返数据库的操作中执行多个查询(只要DB支持,但大多数都可以).在这种情况下,您只会得到count(e.Subordinates) + count(e.Orders)结果,这显然是最小值.

Futures allow you to perform multiple queries in one roundtrip to the database (as long as the DB supports it, but most do). In that case you will only have count(e.Subordinates) + count(e.Orders) results, which is obviously the minimum.

未来将使用标准API,HQL,并且应该使用新的QueryOver API(我尚未测试过).

Futures work with the criteria API, HQL and they are supposed to work with the new QueryOver API (I have not tested that, yet).

NHibernate.Linq确实具有Query().ToFuture()和Query().ToFutureValue(),但是到目前为止,我仅在使用它们时得到异常.

NHibernate.Linq does have Query().ToFuture() and Query().ToFutureValue(), but so far I only get Exceptions when I use them.

我刚刚再次检查了Linq API,如果您不使用Fetch,似乎就可以正常工作.以下将导致三个SQL查询在一次往返中执行.返回的总行数为1 + count(下属)+ count(订单).

I just checked again for the Linq API and it seems as if it is working if you do not use Fetch. The following will result in three SQL queries that are executed in one roundtrip. The total number of rows return will be 1 + count(Subordinates) + count(Orders).

int id = 1;

// get the Employee with the id defined above
var employee = repo.Session.Query<Employee>()
    .Where(o => o.Id == id)
    .ToFuture<Employee>();

// get the Subordinates (these are other employees?)
var subordinates = repo.Session.Query<Employee>()
    .Where(o => o.HeadEmployee.Id == id)
    .ToFuture<Employee>();

// get the Orders for the employee
var orders = repo.Session.Query<Order>()
    .Where(o => o.Employee.Id == id)
    .ToFuture<Order>();

// execute all three queries in one roundtrip
var list = employee.ToList();
// get the first (and only) Employee in the list, NHibernate will have populated the Subordinates and Orders
Employee empl = list.FirstOrDefault();

仍然感谢您将此标记为答案.

Thank you for having marked this as the answer anyway.

这篇关于多次在linq中获取以休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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