Nhibernate projection with child collection

喜你入骨 提交于 2019-12-01 10:06:46

问题


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
}

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;}
}

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>();

My question is, how do I project just SOME of the properties from the ApplicationSettings entity to the ApplicationSettingsDto child collection?


回答1:


I think you might need to do a MutiQuery and bring together the DTO parents and children yourself.



来源:https://stackoverflow.com/questions/6154280/nhibernate-projection-with-child-collection

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