NHibernate 查询映射集合

发布于 2024-09-16 12:21:22 字数 1108 浏览 12 评论 0原文

在我的 ASP.NET Web 应用程序中,我使用 NHibernate 来保存我的“用户”实例,其中每个实例都有一个“条目”集合。这是典型的一对多映射,并且工作得很好。条目的映射代码如下所示:

<bag name="Entries" cascade="all-delete-orphan">
   <key column="UserID" />
   <one-to-many class="MyApp.Entities.Entry, MyApp.Entities" />
</bag>

现在我有一个页面,我想在其中显示包含登录用户的所有条目的网格。为此,我可以简单地将当前用户的“Entries”属性绑定到网格“DataSource”属性。这也工作得很好,但这也意味着网格内置分页功能(Telerik RadGrid)对数据库性能没有任何影响,因为每次显示网格时都会加载所有条目。

因此,我可以应用自定义分页,只获取显示网格当前页面所需的行。典型的 Linq2NHibernate 查询如下所示:

var query = from entry in Session.Linq<Entry>()
                    where entry.User == currentUser
                    select entry;
query.Skip(pageNum * pageSize).Take(pageSize).ToList();

使用这种方法,我需要扩展我的存储库,尽管 NHibernate 已经完成了 User 和 Entry 之间的映射...

我的问题是:如果我使用 LINQ 直接查询我的“Entries”集合“用户”对象 - 这是否意味着所有条目都将从数据库加载,然后在内存中过滤,或者将其转换为真正的“数据库”查询,以便我可以使用这种更舒适的方法来实施分页?

示例:

myGrid.DataSource = currentUser.Entries.Skip(pageNum * pageSize).Take(pageSize).ToList();

J4I:当然,我在映射文件中使用延迟加载...

提前谢谢您!

In my ASP.NET web-application I use NHibernate to persist my "User"-Instances, where each of them has a "Entries" - collection. It is a typical one-to-many mapping and it works just fine. The mapping-code for the entries looks like this:

<bag name="Entries" cascade="all-delete-orphan">
   <key column="UserID" />
   <one-to-many class="MyApp.Entities.Entry, MyApp.Entities" />
</bag>

Now I have a page, where I want to display a grid with all the entries of the logged-in user. To do so, I could simply bind the "Entries" property of the current user to the Grids "DataSource" - property. This also works just fine, but this also means, that the grids built-in paging-functionality (Telerik RadGrid) doesn't have any effect on database-performance, because all the entries will be loaded each time when displaying the grid.

Therefore I could apply my custom-paging, where I only fetch the rows which I need to display the grids current page. A typical Linq2NHibernate query looks like this:

var query = from entry in Session.Linq<Entry>()
                    where entry.User == currentUser
                    select entry;
query.Skip(pageNum * pageSize).Take(pageSize).ToList();

Using this approach I need to extend my repository altough NHibernate has already done the mapping between User and Entry...

My question is: If I use LINQ to directly query the "Entries"-collection of my "User"-object - does this mean, that all the Entries will be loaded from the database and then filtered in memory or would this be translated to a real "database"-query, so that I could use this much more comfortable approach to implement paging?

Example:

myGrid.DataSource = currentUser.Entries.Skip(pageNum * pageSize).Take(pageSize).ToList();

J4I: Of course I use lazy-loading in the mapping files...

Thank you in advance!

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

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

发布评论

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

评论(1

财迷小姐 2024-09-23 12:21:22

集合上的 LINQ 将始终是 LINQ 到对象,因为它们不实现 IQueryable,因此您将在内存中加载所有内容。

查询是目前唯一可能的方法。

LINQ on the collection will always be LINQ-to-objects, as they don't implement IQueryable, so you'd be loading everything in memory.

A query is the only possible approach at this moment.

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