NHibernate预测 - 如何从关联中投射值

时间:2012-07-02 07:34:56

标签: nhibernate

我试图从实体的关联中投射单个值而没有运气。在下面的查询中只需要来自TrainerAddress的城市名称。培训师地址被映射为培训师的一个组成部分。

session.QueryOver<Trainer>()
   .JoinAlias(x=>x.TrainerAddress.City, ()=> cityAlias, JoinType.LeftOuterJoin)
   .OrderBy(x => x.Name).Asc
   .SelectList(list => list
        .Select(x => x.Id).WithAlias(() => dto.Id)
        .Select(x => x.Name).WithAlias(() => dto.Name)
        .Select(x => x.TrainerAddress.City.Name).WithAlias(() => dto.City))//issue projecting here
  .TransformUsing(Transformers.AliasToBean<TrainerDTO>())
  .List<TrainerDTO>();

这可能吗?

1 个答案:

答案 0 :(得分:2)

您是为TrainerAddress.City指定了别名,但是您没有在任何地方使用该别名? 我不是百分百确定NHibernate是否在QueryOver中支持组件,我知道使用Criteria时它有一些问题,但也许这会有效:

session.QueryOver<Trainer>()
   .JoinAlias(x=> x.TrainerAddress.City, ()=> cityAlias, JoinType.LeftOuterJoin)
   .OrderBy(x => x.Name).Asc
   .SelectList(list => list
        .Select(x => x.Id).WithAlias(() => dto.Id)
        .Select(x => x.Name).WithAlias(() => dto.Name)
        .Select(x => cityAlias.Name).WithAlias(() => dto.City))
  .TransformUsing(Transformers.AliasToBean<TrainerDTO>())
  .List<TrainerDTO>();