title: NHibernate.Linq count throws NHibernate.QueryException : could not resolve property
body: <p>I'm building an ecommerce site using S#arp Architecture. 
  I'm trying to map a hierachy of categories and retrieve the top level categories.
  I'm using NHibernate.Linq for this. 
  I have the following entity:</p>
  
  <pre><code>public class Category : Entity
  {
      #region Properties
      [DomainSignature]
      [NotNullNotEmpty]
      public virtual string Name { get; set; }
      public virtual string Description { get; set; }
      public virtual int ListOrder { get; set; }
  
      public virtual IList&lt;Product&gt; Products { get; set; }
      public virtual IList&lt;Category&gt; ParentCategories { get; set; }
      public virtual IList&lt;Category&gt; ChildCategories { get; set; }
      #endregion
  
      public Category()
      {
          Products = new List&lt;Product&gt;();
          ParentCategories = new List&lt;Category&gt;();
          ChildCategories = new List&lt;Category&gt;();
      }
  }
  </code></pre>
  
  <p>with the following Fluent NHibernate mapping:</p>
  
  <pre><code>public class CategoryMap : ClassMap&lt;Category&gt;
  {
      public CategoryMap()
      {
          Id(x =&gt; x.Id);
          Map(x =&gt; x.Name);
  
          HasManyToMany(p =&gt; p.Products)
              .Cascade.All()
              .Table("CategoryProduct");
  
          HasManyToMany(c =&gt; c.ParentCategories)
              .Table("CategoryHierarchy")
              .ParentKeyColumn("Child")
              .ChildKeyColumn("Parent")
              .Cascade.SaveUpdate()
              .AsBag();
  
  
          HasManyToMany(c =&gt; c.ChildCategories)
              .Table("CategoryHierarchy")
              .ParentKeyColumn("Parent")
              .ChildKeyColumn("Child")
              .Cascade.SaveUpdate()
              .Inverse()
              .LazyLoad()
              .AsBag();
      }
  }
  </code></pre>
  
  <p>I want to retrieve the root categories. I know I have eight in my db so here's my test:</p>
  
  <pre><code>[Test]
  public void Can_get_root_categories()
  {
      // Arrange
      var repository = new CategoryRepository();
  
      // Act
      var rootCategories = repository.GetRootCategories();
  
      // Assert
      Assert.IsNotNull(rootCategories);
      Assert.AreEqual(8, rootCategories.Count());
  }
  </code></pre>
  
  <p>I figure I just get all Categories where the ParentCategories list is empty (initialized in the ctor of Category). So here's my repository method:</p>
  
  <pre><code>public IQueryable&lt;Category&gt; GetRootCategories()
  {
      var session = NHibernateSession.Current;
  
      // using NHibernate.Linq here
      var categories = from c in session.Linq&lt;Category&gt;()
                       where c.ParentCategories.Count == 0
                       select c;
      return categories;
  }
  </code></pre>
  
  <p>When I run my test I get "NHibernate.QueryException : could not resolve property: ParentCategories.Id of: MyStore.Core.Category"</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Here are related questions, but didn't quite solve my problem:</p>
  
  <p><a href="https://stackoverflow.com/questions/1811316/fluent-nhibernate-need-help-with-manytomany-self-referencing-mapping">Fluent nHibernate: Need help with ManyToMany Self-referencing mapping</a></p>
  
  <p><a href="https://stackoverflow.com/questions/3010634/querying-a-self-referencing-join-with-nhibernate-linq">Querying a self referencing join with NHibernate Linq</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2054689/fluent-nhibernate-manytomany-self-referencing-mapping">Fluent NHibernate: ManyToMany Self-referencing mapping</a></p>
  
  <p>Edit: </p>
  
  <p>I think the problem lies with the .count in the Linq expression. <a href="https://stackoverflow.com/questions/2158866/linq-to-nhibernate-count">This post</a> related to this, but I'm not sure how to progress...</p>