title: Flitering an object child collection by a property using Fluent nHibernate
body: <p>Please excuse my ignorance, I am new to nHibernate and am having a conceptual problem trying to filtering an child collection with an nHibernate query</p>
  
  <p>My object model contains two entities User and Task set up like this</p>
  
  <pre><code>public class User
  {
      public User()
      {
          this.Tasks = new List&lt;Task&gt;();
      }
  
      public User(int id): this()
      {
          this.Id = id;
      }
  
      public virtual int Id { get; private set; }
  
      public virtual IList&lt;Task&gt; Tasks { get; set; }
  }
  
  public class Task
  {
      public Task() { }
  
      public Task(int id, bool active): this()
      {
          this.Id = id;
          this.Active = active;
      }
  
      public virtual int Id { get; set; }
  
      public virtual bool Active { get; set; }
  }
  </code></pre>
  
  <p>My nHibernate mappings for these are as follows</p>
  
  <pre><code>public class UserMap: ClassMap&lt;User&gt;
  {
      public UserMap()
      {
          Table("user");
          Id(x =&gt; x.Id);
          HasMany(x =&gt; x.Tasks);
      }
  }
  
  public class TaskMap : ClassMap&lt;Task&gt;
  {
      public TaskMap()
      {
          Table("task");
          Id(x =&gt; x.Id);
          Map(x =&gt; x.Active);
      }
  }
  </code></pre>
  
  <p>My database has two tables 'task' and 'user' which I have filled thusly</p>
  
  <pre><code>SELECT * FROM task;
  +----+--------+---------+
  | Id | Active | User_id |
  +----+--------+---------+
  |  1 |      1 |       3 |
  |  2 |      1 |       3 |
  |  3 |      1 |       3 |
  |  4 |      0 |       3 |
  |  5 |      0 |       3 |
  |  6 |      1 |       1 |
  |  7 |      1 |       1 |
  |  8 |      1 |       1 |
  |  9 |      0 |       1 |
  | 10 |      0 |       1 |
  +----+--------+---------+
  10 rows in set
  
  SELECT * FROM user;
  +----+
  | Id |
  +----+
  |  1 |
  |  2 |
  |  3 |
  |  4 |
  |  5 |
  +----+
  5 rows in set
  </code></pre>
  
  <p>What I am trying to do is run a query that returns a specific user with only the active tasks in its 'Tasks' collection</p>
  
  <pre><code>var query = QueryOver.Of&lt;User&gt;()
      .Where(u =&gt; u.Id == 3)
      .JoinQueryOver&lt;Task&gt;(x =&gt; x.Tasks)
      .Where(t =&gt; t.Active == true);
  
  var results = dataProvider.ExcecuteQuery&lt;User&gt;(query);
  </code></pre>
  
  <p>When I execture this query I expect to get back a single user object with 3 Task objects in its Tasks collection, instead what I get is 3 copies of the same task object (task.Id = 3) all of which have all 5 Tasks in their respective collections. </p>
  
  <p>Is what I am trying to do actually possible or should I just be querying on the Task entity instead?</p>
  
  <p>I hope this is not the case as it would be nice to be able to just see a users active tasks without having to messing around with manual filtering.</p>