So I've spent the last few hours looking for an answer and I can't seem to find anything that makes sense.
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; }
}
What I'm trying to do is get a list of players and the points they've earned across a multitude of different games (there are multiple entities above game
that contain lists of games). So the end result of the query would be List<PlayerSummary>
The SQL I'm looking for would look something like this:
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
Note, I will also need to do some querying/summing against the structure entity which is why it's included.
I'm trying to do this with NHibernate, using the TypeSafe stuff, and my plan is for the application to be database agnostic, so I can't use direct SQL (currently it's using Postgres, but I may move to SQL server at some point).
I don't particularly want to use the "HQL" stuff where you use those magic strings, so I'm trying to use Linq or QueryOver/Query.
Can anyone point me in the right direction?
QueryOver
, notQuery
. The latter does not require explicit joins. Second, HQL will give you a much cleaner query (less code than your SQL), so "keep everything the same" is not a good reason. Third, HQL is type safe; you are confusing type safety with intellisense. – Spelling