Nhibernate投影与儿童集合

时间:2011-05-27 15:16:49

标签: nhibernate nhibernate-criteria nhibernate-projections

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

我的代码选择JUST the Application并预测它是这样的(使用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子集合?

1 个答案:

答案 0 :(得分:3)

我认为你可能需要做一个MutiQuery并自己把DTO的父母和孩子聚集在一起。