绑定获取许多在Linq到NHibernate [英] Bound FetchMany in Linq to NHibernate
问题描述
我正在对某些查询使用FetchMany,NHibernate Profiler给我以下错误:
I am using FetchMany for some of my queries and the NHibernate profiler gives me the following error:
警告:
firstResult/maxResults 用collection fetch指定; 在内存中应用!
WARN:
firstResult/maxResults specified with collection fetch; applying in memory!
我猜这是因为获取未绑定.有解决办法吗?
I guess this is because the fetch is unbound. Is there a solution to this?
推荐答案
之所以会出现此问题,是因为使用FetchMany
会将整个结果集存储到内存中,然后采用指定的子集(有些效率低下,并且可能有危险).
This problem arises because using FetchMany
will bring the whole result set to memory and then Take the specified subset (something inefficient and potentially dangerous).
显然,使用FetchMany
时,没有办法在获取之前限制子集.
Apparently there is no way to limit the subset before fetching when using FetchMany
.
解决方案是使用JoinQueryOver
或JoinAlias
(在其他SO问题中已经讨论了两者的区别)
The solution is to use either a JoinQueryOver
or a JoinAlias
(differences of the two have been discussed in other SO questions)
如此做
Session.QueryOver<Product>().FetchMany(p=>p.Images).Take(5)
会在问题中发出警告,我们这样做
which produces the warning in the question, we do
Image image = null;
Session.QueryOver<Product>().Left.JoinQueryOver(x => x.Images, () => image).Take(5)
产生SELECT TOP (5)
查询
这篇关于绑定获取许多在Linq到NHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!