Nhibernate Mappings with group by SQL

时间:2016-05-24 22:47:09

标签: c# nhibernate fluent-nhibernate queryover

我有以下表格

播放器

  • 编号
  • 名字
  • 姓氏

得分

  • 编号
  • 得分
  • PlayerId

我希望得分最高的foreach玩家,我在我的存储库中做错了,因为我只能获得最高分。

我试过这个

 var result= session.QueryOver<ScoreRecord>()
     .JoinQueryOver(p => p.Player)
     .SelectList(list => list
         .Select(s => s.Player)
         .SelectMax(s => s.Score))
     .List<object>();

上面没有加入。这个

var ranking = session.CreateCriteria<ScoreRecord>()
    .SetProjection(
        Projections.ProjectionList()
            .Add(Projections.Property("Score"), "Score")
            .Add(Projections.Property("Player"), "Player"))
    .SetProjection(
        Projections.Max("Score"))
        .SetResultTransformer(new AliasToBeanResultTransformer(typeof(ScoreRecord)))
    .List<ScoreRecord>();

没有成功。有什么想法吗?

非常感谢 谢谢 库尔蒂斯

**编辑** 以下是分数和玩家等级的映射。

public class ScoreRecord
{
    public virtual int Id { get; set; }
    public virtual decimal Score { get; set; }
    public virtual PlayerRecord Player { get; set; }
}

public class ScoreMap : ClassMap<ScoreRecord>
{

    public ScoreMap()
    {
        Id(x => x.Id);
        Map(x => x.Score);
        References(x => x.Player).Column("PlayerId").Not.Nullable();
        Table("tbl_Lcc_Score");
    }
}

public class PlayerRecord
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string Surname { get; set; }
    public virtual string Email { get; set; }
    public virtual bool NewsletterOptIn { get; set; }
    public virtual IList<ScoreRecord> Scores { get; set; }
    public virtual GenderRecord Gender { get; set; }

    public PlayerRecord()
    {
        Scores = new List<ScoreRecord>();
    }
}

public class PlayerMap : ClassMap<PlayerRecord>
{
    public PlayerMap()
    {
        Id(x => x.Id);
        Map(x => x.FirstName);
        Map(x => x.Surname);
        Map(x => x.Email).Unique();
        Map(x => x.NewsletterOptIn);
        HasMany(x => x.Scores).Cascade.All();
        References(x => x.Gender).Column("GenderId");
        Table("tbl_Lcc_Player");
    }
}

由于

0 个答案:

没有答案