Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is to my knowledge no easy way to achieve this, however have a look at PredicateBuilder, which does solve some of these issues: <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" rel="nofollow noreferrer">http://www.albahari.com/nutshell/predicatebuilder.aspx</a>. </p> <p>The basic idea is that you define interfaces with the common properties, which allows you to define generic expressions constrained to that interface.</p> <p>Start by definining these interfaces:</p> <pre><code>public interface IHaveItem { Item Item {get;} } public interface IDateVisible { DateTime? DateVisible {get;} } </code></pre> <p>This will allow you to write the query expressions in a generic way, obtaining at least some reuse:</p> <pre><code>public static Expression&lt;Func&lt;T,bool&gt;&gt; IsVisible&lt;T&gt;(DateTime now) where T:IHaveItem { return x =&gt; x.Item.DateVisible != null &amp;&amp; x.Item.DateVisible &lt; now; } </code></pre> <p>You will still not be able to easily achieve reuse between expressions like <code>x.DateVisible</code> and <code>x.Item.DateVisible</code> however.</p> <p>UPDATE: it turns out that 'unnecessary' cast operations are being added in the generated expression tree, which are then not supported by the entity framework provider when translating to SQL.</p> <p>You can apply a similar trick as the one in <a href="https://stackoverflow.com/questions/9120593/iqueryablet-with-entityobject-using-generics-interfaces-possible">this question</a> to remove these unnecessary cast operations 'manually'. Becomes a bit hairy though IMHO...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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