7

所以我在过去的几个小时里一直在寻找答案,但我似乎找不到任何有意义的东西。

public class Game
{
   public virtual Guid ID { get; set; }
   public virtual ResultStructure Structure { get; set; }
   public virtual List<Result> Results { get; set; }
}

public class Result
{
  public virtual Player Player { get; set; }
  public virtual int Position { get; set; }
}

public class ResultStructure
{
  public virtual Guid ID { get; set; }
  public virtual List<ResultOutcomes> Outcomes { get; set;}
}

public class ResultOutcomes
{
  public virtual int Position { get; set; }
  public virtual int Points { get; set; }
}

public class PlayerSummary
{
  public virtual Player Player { get; set; }
  public virtual int Points { get; set; }
}

我要做的是获取玩家列表以及他们在众多不同游戏中获得的积分(上面有多个game包含游戏列表的实体)。所以查询的最终结果将是List<PlayerSummary> 我正在寻找的 SQL 看起来像这样:

SELECT p.*, Sum(rs.points) FROM result r
  JOIN player p on r.playerid = p.id
  JOIN game g on r.gameid = g.id
  JOIN resultstructure rs on g.resultstructureid = rs.id
  JOIN resultoutcomes ro on rs.id = ro.resultstructureid AND ro.position = r.position

请注意,我还需要对结构实体进行一些查询/求和,这就是包含它的原因。

我正在尝试使用 NHibernate 执行此操作,使用 TypeSafe 的东西,我的计划是让应用程序与数据库无关,所以我不能使用直接 SQL(目前它正在使用 Postgres,但我可能会在某些时候转移到 SQL 服务器观点)。

我不想在你使用那些魔术字符串的地方使用“HQL”的东西,所以我正在尝试使用 Linq 或 QueryOver/Query。

谁能指出我正确的方向?

4

1 回答 1

6

似乎上述情况在我的情况下是可能的,因为存在关系,只是不直接。

您可以使用JoinAlias.

基本的区别在于,使用JoinAlias,您可以将多个表连接到同一个基表,其中JoinQueryOver通过将每个表仅连接到前一个表的表需要线性进展。

所以查询看起来像这样。

Result resultAlias = null;
ResultOutcome outcomeAlias = null;
ResultStructure structureAlias = null;

var results = Session.QueryOver(() => resultAlias) // Assigns resultAlias so it can be used further in the query.
   .Inner.JoinQueryOver(x => x.Game) // returns a QueryOver Game so you can do a where on the game object, or join further up the chain.
   .Inner.JoinAlias(x => x.ResultStructure, () => structureAlias) // joins on the Structure table but returns the QueryOver for the Game, not the structure.
   .Inner.JoinAlias(() => structureAlias.Outcomes, () => outcomeAlias) // same again for the outcomes
   .Where(() => resultAlias.Position == outcomeAlias.Position)
   .Select(
        Projections.Group(() => resultAlias.Player),
        Projections.Sum(() => outcomeAlias.Points)
   );

这应该给人们这个想法。这样做的缺点是对“位置”的限制不会发生在 Join 上,而是发生在 Where 子句中。我很高兴听到任何可以选择这样做的人的来信,因为这会迫使数据库查询规划器沿着特定的路线前进。

仍在处理转换和排序,但这让我更进一步。

于 2013-02-14T18:21:18.897 回答