title: Complex queries in linq to nhibernate
body: <p>We are using <a href="http://martinfowler.com/apsupp/accountability.pdf" rel="nofollow noreferrer">accountability pattern</a> for organizational structure. I using linq to nhibernate to find some departments and position but I have two problem.</p>
  
  <pre><code>var query = 
    Repository&lt;Party&gt;.Find(p =&gt; p.IsInternal == true)
      .Where(p =&gt; p.Parents.Any(c =&gt; c.Parent.PartyId == id))
      .Where(p =&gt; 
        (
          p.PartyType.PartyTypeId == (int)PartyTypeDbId.Department &amp;&amp; 
          _secretariat.Departments.Select(c =&gt; c.PartyId).Contains(p.PartyId)
        )
        || 
        (
          p.PartyType.PartyTypeId == (int)PartyTypeDbId.Position &amp;&amp; 
          p.Children.Any(c =&gt; 
            c.AccountabilityType.AccountabilityTypeId == (int)AccountabilityTypeDbId.TenurePersonOfPosition &amp;&amp;  
            ((Person)c.Child).UserName != null)
          )
      );
  </code></pre>
  
  <p>First : I got 'Unhandled Expression Type: 1003' for this part of query : '_secretariat.Departments.Select(c => c.PartyId).Contains(p.PartyId)'
  and I got Property not found 'UserName'</p>
  
  <p>We have many complex queries i think we need to use stored procedure.</p>
  
  <p>Sorry for bad Inglish!</p>