NHibernate 查询映射集合
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
集合上的 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.