使用投影在QueryOver中加入集合

时间:2015-02-27 15:18:46

标签: c# nhibernate outer-join queryover nhibernate-projections

我想要检索带有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那样的预计dtos,并为每个字符条目折叠重复的用户数据?

1 个答案:

答案 0 :(得分:1)

您正尝试投影到两种不同的对象类型(UserDtoCharacterDto)。我认为它不受支持。如果你查看你的代码,你告诉NHibernate他应该使用UseDtoTransformers.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关键字)