1

我想User用他们的Characters集合检索短版本列表(短版本也是如此)。我将 QueryOver 与投影一起使用。

public class User
{
        public virtual int Id { get; set; }
        public virtual string Nickname { get; set; }
        public virtual ISet<Character> Characters { get; set; }
        // and 30 other properties
}

public class Character
{
        public virtual int Id { get; set; }
        public virtual int XP { get; set; }
        public virtual int UserId { get; set; }
        public virtual int ClassId { get; set; }
        // and 30 other properties
}

public class UserDto // short version
{
        public virtual string Nickname { get; set; }
        public virtual ISet<CharacterDto> Characters { get; set; }
}


public class CharacterDto // short version
{
        public virtual int XP { get; set; }
        public virtual int ClassId { get; set; }
}

    User userAlias = null;
    UserDto userDto = null;
    Character characterAlias = null;
    CharacterDto characterDto = null;

    var result = session.QueryOver<User>(() => userAlias)
            .Where(Restrictions.Like(
                Projections.SqlFunction("lower", NHibernateUtil.String, Projections.Property<User>(x => x.Nickname)),
                nickname.ToLowerInvariant(), MatchMode.Start))
            .JoinAlias(x => x.Characters, () => characterAlias, JoinType.LeftOuterJoin)
             .Select(
                 Projections.Property(() => userAlias.Nickname).WithAlias(() => userDto.Nickname),
                 Projections.Property(() => userAlias.Characters).WithAlias(() => userDto.Characters),
                 Projections.Property(() => characterAlias.XP).WithAlias(() => characterDto.XP),
                 Projections.Property(() => characterAlias.ClassId).WithAlias(() => characterDto.ClassId)
                )
            .TransformUsing(Transformers.AliasToBean<UserDto>())
             .Take(50)
             .List<UserDto>();

当我运行此代码时,它会引发异常:Could not find a setter for property ClassId in class UserDto. 如果我删除转换器,则结果包含每个用户一个字符的条目(User1-Character1,User1-Character2,...)。每个条目都有正确的昵称和字符类 ID,但错误的 xp(而不是用户 ID)和字符集合(== null)。

如何在不使用 hql 或单独查询字符的情况下获得正确的数据?

更新

我删除了转换器并注释掉了Projections.Property(() => userAlias.Characters)...,现在它返回了正确的结果,但是有没有办法将它们转换为像 AliasToBeanTransformer 那样的投影 dto 并折叠每个字符条目的重复用户数据?

4

1 回答 1

2

您正在尝试投影到两种不同的对象类型(UserDtoCharacterDto)。我不认为它被支持。如果你查看你的代码,你告诉 NHibernate 他应该使用UseDto( Transformers.AliasToBean<UserDto>()) 但你没有告诉它关于CharacterDto.

参见例如http://blog.andrewawhitaker.com/blog/2014/06/19/queryover-series-part-4-transforming/

您无法填充集合(例如,如果您有一个具有 ProductID 的类和一个 ProductReviews 集合,则您无法使用 AliasToBean 一步完成)

你可以做什么:

添加一个包含所有内容的类 ( UserCharacterDto)

public class UserCharacterDto // short version
{
    public virtual int Id { get; set; }
    public virtual string Nickname { get; set; }
    public virtual int? XP { get; set; }
    public virtual int? ClassId { get; set; }
}

public class UserDto // short version
{
    public virtual string Nickname { get; set; }
    // Changed from ISet: you don't have a key!
    public virtual IList<CharacterDto> Characters { get; set; }
}

public class CharacterDto // short version
{
    public virtual int XP { get; set; }
    public virtual int ClassId { get; set; }
}

首先你投射到UserCharacterDto,

UserCharacterDto uc = null;

IList<UserCharacterDto> result = ...

    .Select(
        Projections.Property(() => userAlias.Nickname).WithAlias(() => uc.Nickname),
        Projections.Property(() => characterAlias.XP).WithAlias(() => uc.XP),
        Projections.Property(() => characterAlias.ClassId).WithAlias(() => uc.ClassId)
    )
    .TransformUsing(Transformers.AliasToBean<UserDto>())
    .Take(50)
    .List<UserCharacterDto>()

然后你GroupBy,最后你创造你的元素

IList<UserDto> result2 = (from x in result
                          // Here you group by User
                          group x by x.Id into y
                          let first = y.First()
                          // Here you build the "definitive" UserDto
                          select new UserDto {
                              Nickname = first.Nickname,
                              // and here you build the "definitive" CharacterDto
                              // note the handling of empty objects!
                              Characters = new List<CharacterDto>(y.Select(z => z.XP != null && z.ClassId != null ? new CharacterDto { XP = z.XP.Value, ClassId = z.ClassId.Value } : null))
                          }
                         ).ToList();

// remove empty CharacterDto from left join
foreach (UserDto user in result2) {
    if (user.Characters.Count == 1 && user.Characters[0] == null) {
        user.Characters.Clear();
    }
}

(请注意,我没有使用 LINQ 的函数式语法,因为我想使用let关键字)

于 2015-02-27T15:36:55.687 回答