NHibernate查询CreateCriteria [英] NHibernate query CreateCriteria
问题描述
是否可以选择要从Session.CreateCriteria()返回的列?
Is it possible to chose what columns I want in return from Session.CreateCriteria() ?
例如.
var x = session.CreateCriteria();
x.CreateAlias("EmployeePosition", "employeePosition");
x.Add(Restrictions.Eq("employeePosition.Name", "Developer"));
并且有一种方法可以添加诸如选择姓氏"之类的内容,以避免下载整个行.
and is there a way to add something like "select LastName" to avoid downloading the whole row.
推荐答案
创建仅具有所需属性的类,通常这是一个摘要类,例如{Id,Label},您可以在需要的任何地方重用它简单类型,例如在清单中.使用ProjectionList定义要返回的列.然后使用Transformers.AliasToBean将结果转换为您的简单类型.
make a class that has only the properties you need, often this is a summary class like {Id, Label} and you'd reuse it anywhere you need a simple type, in a listing for example. Use ProjectionList to define which columns to return. Then use Transformers.AliasToBean to transform the result to your simple type.
ProjectionList projectionList = Projections.ProjectionList();
projectionList.Add(Projections.Property("EmployeeID"), "Id");
projectionList.Add(Projections.Property("EmployeePosition"), "Label");
var x = DetachedCriteria.For(Employee);
x.SetProjection(projectionList);
x.SetResultTransformer(Transformers.AliasToBean(SimpleType)));
return x.GetExecutableCriteria(UnitOfWork.CurrentSession).List<SimpleType>();
这篇关于NHibernate查询CreateCriteria的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!