NHibernate Session.Query<>, column list?

房东的猫 提交于 2019-12-25 03:52:40

问题


We have some code written which depends on working with the IQueryable instances, so I suppose we are stuck with having to use ISession.Query<>().

In one particular case I would like to only partially hydrate the DBOs and exclude certain columns from the SELECT statement which NHibernate will generate. Is it possible to achieve that while using Query<>?

Alternatively, is it possible to somehow go from ICriteria to IQueryable? (I think for ICriteria it is possible to achieve what I need by using Projections?)


回答1:


Projections are supported in IQueryable as well. Syntax should be like this:

var query = session.Query<Employee>();
var list = query.Select(s => new Employee
    {
        FirstName = s.FirstName,
        LastName = s.LastName,
        ...
     })
     .ToList();

The new Employee could be even some DTO...

Some basic info about QueryOver projection API:

  • 16.6. Projections


来源:https://stackoverflow.com/questions/25971199/nhibernate-session-query-column-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!