NHibernate 挂在延迟加载查询上

发布于 2024-08-14 11:44:57 字数 849 浏览 3 评论 0原文

首先,我使用 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 挂在这个声明上。我一开始以为是因为数据库中有太多记录(大约500,000条)。

但我将过滤结果的行更改为:

results = SessionFactoryProvider.SessionFactory.CurrentSession.Linq(Of Customer).Where(Function(c) c.Name.Contains(OwnerName))

并且它的工作速度非常快。但我似乎无法弄清楚为什么。有什么想法吗?

First of all, I'm using Fluent NHibernate with LinqToNHibernate.

I've got a query to do a search on a table based on what data the user entered. So, for example, I'm doing something like this:

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

So essentially I'm building a where statement on the sql statement if the user wants to search by name. I'm using all lazy configurations in my mappings, so the query shouldn't be retrieving the items until the "ToList()" is called. The problem is that NHibernate hangs on this statement. I thought at first it was because there were so many records in the db(about 500,000).

But I changed the line where I am filtering the results to this:

results = SessionFactoryProvider.SessionFactory.CurrentSession.Linq(Of Customer).Where(Function(c) c.Name.Contains(OwnerName))

And it works very quickly. But I can't seem to figure out why. Any ideas?

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

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

发布评论

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

评论(1

婴鹅 2024-08-21 11:44:57

在第一种情况下,您将检索所有记录并使用 Linq-to-objects 来过滤该列表。

在第二种情况下,您将查询发送到数据库(NHibernate.Linq 将表达式转换为 SQL WHERE 子句)。

现在,我不知道 GetCustomers 的返回类型是什么,但由于您将其存储到 IEnumerable(Of Customer) 中,因此 .NET 无法了解底层提供程序。如果 GetCustomers 的代码类似于:

Return CurrentSession.Linq(Of Customer)

...那么问题就出在该转换上。只需将第一行更改为:

Dim results As IQueryable(Of Customers) = Me.GetCustomers()

In the first case you are retrieving all the records and using Linq-to-objects to filter that list.

In the second case, you are sending the query to the database (NHibernate.Linq converts your expression into a SQL WHERE clause).

Now, I don't know what is the return type of GetCustomers, but since you're storing it into an IEnumerable(Of Customer), .NET has no way of knowing about an underlying provider. If GetCustomers' code is something like:

Return CurrentSession.Linq(Of Customer)

...Then the problem is with that conversion. Just change the first line to:

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