Note that there are some explanatory texts on larger screens.

plurals
  1. POBitwise operation and Extension method with NHibernate QueryOver
    text
    copied!<p>I want to query a table with numeric columns that contains flags.</p> <p>I've built a mapping with NHibernate XML against a SQL Server 2008 database (I show you only interesting part):</p> <p>This is my enum:</p> <pre><code>[Flags] public enum DocsFlag { FLAG_TOREAD = 0, FLAG_READ = 2, FLAG_MANAGED = 4, FLAG_INUSE = 8, FLAG_FORWARDED = 16, FLAG_RESUBMITTED = 32, FLAG_PRINTED = 64, } </code></pre> <p>this is my class:</p> <pre><code>public class MyObject { ... public virtual DocsFlag Flags { get; set; } } </code></pre> <p>Here my class db mapping:</p> <pre><code>&lt;class name="MyObject"&gt; ... &lt;property name="Flags" not-null="true"/&gt; &lt;/class&gt; </code></pre> <p>I want to query with strongly typed <code>QUERYOVER</code> :</p> <pre><code>_session.QueryOver&lt;MyObject&gt;() .Where(x =&gt; x.Error != null &amp; (x.Flags &amp;&amp; (DocsFlag.FLAG_READ | DocsFlag.FLAG_MANAGED | DocsFlags.FLAG_PRINTED)&gt; 0)) .Left.JoinQueryOver&lt;DestinationObj&gt;(m =&gt; m.Destination) .Left.JoinQueryOver&lt;UsersObj&gt;(x =&gt; x.LinkedUser).ToList(); </code></pre> <p>Or simply:</p> <pre><code>.Where (x=&gt; x.Flags &amp;&amp; (DocsFlag.FLAG_READ | DocsFlag.FLAG_MANAGED | DocsFlags.FLAG_PRINTED)&gt; 0) </code></pre> <p>But it doesn't work....</p> <p><strong>What is the best way to query bit flags with nhibernate <code>QueryOver</code>?</strong></p> <p>I tried these solutions:</p> <ol> <li><p>I think to query directly with <code>createSqlQuery</code> but my query are very complex and I want to take advantage of <code>QueryOver</code> API.</p></li> <li><p>I have begin my project with Linq to Nhibernate, but I've found a lot of bugs and I have noticed that there are not full support for query...</p></li> <li><p>Write my own <code>QueryOver</code> extension methods to query flags columns but it's too early for me because I didn't understand how to <code>QueryOver</code> API transform code lambda functions in SQL query (and apply appropriate dialect).</p></li> </ol> <p>Help me to find the best way...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload