【发布时间】:2009-12-14 20:57:40
【问题描述】:
首先,我将 Fluent NHibernate 与 LinqToNHibernate 一起使用。
我有一个查询,可以根据用户输入的数据对表进行搜索。所以,例如,我正在做这样的事情:
'build the initial query that we will filter--this is lazy loaded
Dim results As IEnumerable(Of Customers) = Me.GetCustomers()
'filter by owner name
If String.IsNullOrEmpty(OwnerName) = False Then
results = results.Where(Function(s) s.Name.Contains(OwnerName))
End If
'execute query
results = results.ToList()
因此,如果用户想按名称搜索,基本上我会在 sql 语句上构建 where 语句。我在我的映射中使用了所有惰性配置,因此在调用“ToList()”之前,查询不应该检索项目。问题是 NHibernate 挂在这个声明上。一开始我以为是因为db里的记录太多了(大约50万条)。
但我将过滤结果的那一行更改为:
results = SessionFactoryProvider.SessionFactory.CurrentSession.Linq(Of Customer).Where(Function(c) c.Name.Contains(OwnerName))
而且它的工作速度非常快。但我似乎无法弄清楚为什么。有什么想法吗?
【问题讨论】:
标签: nhibernate fluent-nhibernate lazy-loading