0

我有三张桌子。一个是主表:TableA。一个表被称为 ReferencedTable 的 TableA 引用,最后一个由 ReferencedTable 引用的查找表。

我有这个查询,它返回十个最近的对象:

TableADTO TableAlias = null;
LookupTableDTO LookupTableAlias = null;
ReferencedDTO ReferencedAlias = null;

dtos = session.QueryOver(() => TableAlias)
          .JoinAlias(() => TableAlias.Object, () =>ReferencedAlias)
          .JoinAlias(() => ReferencedAlias.ObjectType, () => LookupTableAlias)
          .Where(() => ReferencedAlias.PersonId == user.Id &&
                       (LookupTableAlias.Id != INVOICE_ID ||
                        LookupTableAlias.Id != FINANCIAL_ID) &&
                       TableAlias.Status == NEW_STATUS_FLAG &&
                       ReferencedAlias.ReceivedDate < DateTime.Now)
          .Take(10)
          .List()
          .Select(dto=>
          new AbreviatedDTO
          {
              Id = dto.Referenced.Id,
              Field1 = dto.Field1,
              Priority = dto.Referenced.Priority,
              ReceivedDate = dto.Referenced.ReceivedDate,
              Field1 = dto.Referenced.Field1,
              Type = dto.Referenced.Lookup.TypeCode,
              Status = dto.Status
          }).ToList();    

这按预期工作。但是,我认为下面的转换也可以。它确实带来了 10 个对象,但这些对象具有所有默认值且未填充(例如 AbreviatedDTO.ReceivedDate = DateTime.Minimum)。我在 QueryOver 上做错了吗?

任何帮助,将不胜感激。比尔 N

TableDTO TableAlias = null;
LookupTableDTO LookupTableAlias = null;
ReferencedDTO ReferencedAlias = null;

dtos = session.QueryOver(() => TableAlias)
          .JoinAlias(() => TableAlias.Object, () =>ReferencedAlias)
          .JoinAlias(() => ReferencedAlias.ObjectType, () => LookupTableAlias)
          .Where(() => ReferencedAlias.PersonId == user.Id &&
                       (LookupTableAlias.Id != INVOICE_ID ||
                        LookupTableAlias.Id != FINANCIAL_ID) &&
                       TableAlias.Status == NEW_STATUS_FLAG &&
                       ReferencedAlias.ReceivedDate < DateTime.Now)
 .SelectList(list => list
                        .Select(x => TableAlias.Field1)
                        .Select(x => ReferencedAlias.Id)
                        .Select(x => ReferencedAlias.Field1)
                        .Select(x => ReferencedAlias.ReceivedDate)
                        .Select(x => ReferencedAlias.Priority)
                        .Select(x => LookupTableAlias.TypeCode))
.TransformUsing(Transformers.AliasToBean<AbreviatedDTO>())
          .Take(10)
  .List<AbreviatedDTO>()
4

1 回答 1

0

您需要为每个选定字段定义一个别名,该别名与生成的 dto 中的属性名相同

AbreviatedDTO alias = null;

// in query
    .SelectList(list => list
        .Select(() => TableAlias.Field1).WithAlias(() => alias.Field1)
于 2013-02-15T10:55:37.527 回答