title: How to query enum flags using bitwise operators in Linq to NHibernate
body: <p>I have a problem where a bitwise operation on a query like the following generates N+1 selects:</p>
  
  <pre><code>var files = (from f in Session.Query&lt;File&gt;()
      where f.ProjectID == id &amp;&amp; ((f.State &amp; FileState.WaitingForFile) == FileState.WaitingForFile)
      select f;
  </code></pre>
  
  <p>Instead of generating the expected SQL, NHibernate performs N+1 selects (all rows in File) and tests the binary condition on the client. The issue seems to be linked with Lazy loading as NHibernateProfiler shows the following culprit in its stack trace: FileProxy.get_State</p>
  
  <p><strong>How do I get NHibernate to generate the correct SQL (Where ProjectID = x AND State &amp; y = y) ?</strong></p>
  
  <p>Detailed information follows:</p>
  
  <p>I am using NHibernate 3.2.0.4.</p>
  
  <p>Here are the classes I'm using: </p>
  
  <pre><code>public class File
  {
      public virtual int ID { get; set; }
      public virtual int ProjectID { get; set; }
      public virtual AttachedFileType AttachedFileType { get; set; }
      public virtual FileState State { get; set; }
      [...]
  }
  
  [Flags]
  public enum FileState
  {
      None = 0x0,
      Available = 0x1,
      WaitingForFile = 0x2,
      Archived = 0x4,
      [...]
  }
  </code></pre>
  
  <p>Thank you for your time.</p>