title: NHibernate QueryOver query subclass from base
body: <p>I have the following objects:</p>
  
  <pre><code>public abstract class Event {
      public virtual Guid Id { get; set; }
      public virtual string Description { get; set; }
  }
  
  public class TeamEvent : Event {
      public virtual Guid Id { get; set; }
      public virtual Team Team { get; set; }
  }
  
  public class PersonEvent : Event {
      public virtual Guid Id { get; set; }
      public virtual Person  Person { get; set; }
  }
  
  public class Team {
      public virtual Guid Id { get; set; }
      public virtual string Name { get; set; }
  }
  
  public class Person {
      public virtual Guid Id { get; set; }
      public virtual string Forename { get; set; }
      public virtual string Surname { get; set; }
  }
  </code></pre>
  
  <p>And Mappings:
       
          
              
          </p>
  
  <pre><code>    &lt;discriminator column="Discriminator"
              not-null="true"
              type="System.String"/&gt;
  
      &lt;subclass
          name="TeamEvent"
          discriminator-value="TeamEvent"&gt;
          &lt;many-to-one name="Team" column="TeamId"/&gt;
      &lt;/subclass&gt;
  
      &lt;subclass
          name="PersonalEvent"
          discriminator-value="PersonalEvent"&gt;
          &lt;many-to-one name="Person" column="PersonId"/&gt;
      &lt;/subclass&gt;
  &lt;/class&gt;
  &lt;class name="Person" table="Persons"&gt;
      &lt;id name="Id"&gt;
          &lt;generator class="assigned"/&gt;
      &lt;/id&gt;
      &lt;property name="Forename"/&gt;
      &lt;property name="Surname"/&gt;
  &lt;/class&gt;
  &lt;class name="Team" table="Teams"&gt;
      &lt;id name="Id"&gt;
          &lt;generator class="assigned"/&gt;
      &lt;/id&gt;
      &lt;property name="TeamName"/&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>What I'd like to do is a query that would get all events regardless of base type in one query to populate a DTO like:</p>
  
  <pre><code>public class EventDTO {
     public virtual Guid EventId { get; set; }
     public virtual string EventDescription { get; set; }
     public virtual Guid PersonId { get; set; }
     public virtual string PersonForename { get; set; }
     public virtual string PersonSurname { get; set; }
     public virtual Guid TeamId { get; set; }
     public virtual string TeamName { get; set; }
  }
  </code></pre>
  
  <p>Where if it's a person event, the team fields are null and vice versa.</p>
  
  <p>In SQL I'd just do something like:</p>
  
  <pre><code>Select * from Events
  left join Team on Events.TeamId = Team.Id
  left join Person on Events.Person = Person.Id
  </code></pre>
  
  <p>I can't do this in QueryOver as the Events c# object doesn't have properties for Team/Person. I can do it like so:</p>
  
  <pre><code>var events = session.QueryOver&lt;Event&gt;
                 .Where ( /* insert my restrictions */ )
                 .Future&lt;Event&gt;();
  
  var teamEvents = session.QueryOver&lt;TeamEvent&gt;
                 .Where ( /* insert my restrictions */ )
                 .Future&lt;TeamEvent&gt;();
  var personEvents = session.QueryOver&lt;PersonEvent&gt;
                 .Where ( /* insert my restrictions */ )
                 .Future&lt;PersonEvent&gt;();
  
  /* union the results in memory */
  </code></pre>
  
  <p>The amount of data coming back from these queries should be such that it's feasible to just union the results in memory. My question is really is there a more elegant way of doing this? without doing three separate queries.</p>
  
  <p>I should point out this is just a quick example I've typed up to highlight my question so there may be some typos, etc in the above code samples. In the real world problem there is other parts of the system depending on the mappings, so changing the schema/mappings/objects is not an option.</p>
  
  <p>And whilst I'd prefer to use query over, I don't mind using criteria or hql if it's possible to do it in a simpler way there. Also this query will only return a max of 20 rows, of entities that are frequently accessed and thus stored in second level cache, so even an N + 1 isn't a major issue if it makes the code simple.</p>
  
  <p>I should also point out the reason the three queries one isn't so simple is because there needs to be 10 restrictions that need to be duplicated per query, and there are a couple of other subclasses of event.</p>
  
  <p>Thanks for any help.</p>