title: Entity Framework Custom Query Function
body: <p>I have an Entity Framework 4.0 Entity Object called <code>Revision</code> w/ Nullable <code>DateEffectiveFrom</code> and <code>DateEffectiveTo</code> dates. I was wondering if there was a short-hand way of querying an object's <code>RevisionHistory</code> based on a particular <code>QueryDate</code> date instead of having to use the following query structure:</p>
  
  <pre><code>var results = EntityObject.Revisions.Where(x =&gt;
      (x.DateEffectiveFrom == null &amp;&amp; x.DateEffectiveTo == null) ||
      (x.DateEffectiveFrom == null &amp;&amp; x.DateEffectiveTo &gt;= QueryDate) ||
      (x.DateEffectiveFrom &lt;= QueryDate &amp;&amp; x.DateEffectiveTo == null) ||
      (x.DateEffectiveFrom &lt;= QueryDate &amp;&amp; x.DateEffectiveTo &gt;= QueryDate));
  </code></pre>
  
  <p>I've tried creating the following boolean function in the <code>Revision</code> class:</p>
  
  <pre><code>partial class Revision
  {
      public bool IsEffectiveOn(DateTime date)
      {
          return (x.DateEffectiveFrom == null &amp;&amp; x.DateEffectiveTo == null) ||
              (x.DateEffectiveFrom == null &amp;&amp; x.DateEffectiveTo &gt;= date) ||
              (x.DateEffectiveFrom &lt;= date &amp;&amp; x.DateEffectiveTo == null) ||
              (x.DateEffectiveFrom &lt;= date &amp;&amp; x.DateEffectiveTo &gt;= date));
      }
      ...
  }
  </code></pre>
  
  <p>And then updating the query to:</p>
  
  <pre><code>var results = EntityObject.Revisions.Where(x =&gt; x.IsEffectiveOn(QueryDate));
  </code></pre>
  
  <p>but this obviously doesn't translate to SQL.  Any ideas would be much appreciated.</p>