gpt4 book ai didi

带有子集合的 Nhibernate 投影

转载 作者:行者123 更新时间:2023-12-04 06:24:59 26 4
gpt4 key购买 nike

使用 NHibernate 2.1,我试图将一个实体及其子集合投影到 DTO 中。我的实体看起来像这样..

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 看起来像这样..
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)
  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 子集合?

最佳答案

我认为你可能需要做一个 MutiQuery 并将 DTO parent 和 child 自己聚集在一起。

关于带有子集合的 Nhibernate 投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6154280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com