问题描述
我正在使用NHibernate 2.1,将一个实体及其子集合投影到DTO中.我的实体看起来像这样.
Using NHibernate 2.1, I'm trying to project an entity and its child collection into a DTO. My entity looks like this..
public class Application
{
public int Id {get;set;}
public string Name {get;set;}
public List<ApplicationSetting> Settings {get;set;}
// A bunch of other properties that I don't want in the DTO
}
public class ApplicationSetting
{
public int Id {get;set;}
public string Name {get;set;}
public string Code {get;set;}
// A bunch of other properties that I don't want in the DTO
}
我的DTO看起来像这样.
My DTO looks like this..
public ApplicationDto
{
public int Id {get;set;}
public string Name {get;set;}
public List<ApplicationSettingDto> Settings {get;set;}
}
public class ApplicationSettingDto
{
public int Id {get;set;}
public string Name {get;set;}
public string Code {get;set;}
}
我选择应用程序并对其进行投影的代码就是这个(使用Nhibernate 2.1和nhLambdaExtensions)
My code to select JUST the Application and project it is this (using Nhibernate 2.1 and nhLambdaExtensions)
var applicationAlias = new Application();
var criteria = Session
.Add<Application>(a => a.Id == id);
int? Id = null;
string Name = null;
criteria
.SetProjection
(
Projections.Distinct(
Projections.ProjectionList()
.Add(LambdaProjection.Property<Application>(a => a.Id).As(() => Id))
.Add(LambdaProjection.Property<Application>(a => a.Name).As(() => Name))
)
);
criteria.SetResultTransformer(Transformers.AliasToBean(typeof(ApplicationDto)));
var contract = criteria.UniqueResult<ApplicationDto>();
我的问题是,如何仅将一些属性从ApplicationSettings实体投影到ApplicationSettingsDto子集合?
My question is, how do I project just SOME of the properties from the ApplicationSettings entity to the ApplicationSettingsDto child collection?
推荐答案
我认为您可能需要执行MutiQuery并亲自将DTO父母和孩子聚在一起.
I think you might need to do a MutiQuery and bring together the DTO parents and children yourself.
这篇关于带有孩子集合的Nhibernate投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!