1

我试图从一个实体的关联中投射一个单一的价值,但没有运气。在下面的查询中只需要来自 TrainerAddress的城市名称。Trainer 地址被映射为 Trainer 的一个组件。

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

这可能吗?

4

1 回答 1

2

您正在为 TrainerAddress.City 指定别名,但您没有在任何地方使用该别名?我不是 100% 确定 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>();
于 2012-07-02T15:17:39.513 回答