Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have built something similar to what you are looking to construct. I built around using Expression trees from System.Linq.Expressions. So I would suggest that you build your where predicate using an expression tree where the predicate has the signature of <code>Expression&lt;Func&lt;T,bool&gt;&gt;</code>. </p> <p>Some pseudo code below where T is a WCF RIA entity:</p> <pre><code>public static class QueryHelper { public static Expression&lt;Func&lt;T, bool&gt;&gt; ToPredicate&lt;T&gt; ( string propertyName, dynamic criteria ) { ParameterExpression pe = Expression.Parameter( typeof( T ), "t" ); Expression np = Expression.Property( pe, propertyName ); ConstantExpression value = Expression.Constant( criteria ); Expression e1 = Expression.Equal( np, value ); var filter = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( e1, pe ); return filter; } } </code></pre> <p>Which could then be used like:</p> <pre><code>var selector = QueryHelper.ToPredicate&lt;Category&gt;("theColumn", "hello"); return this.ObjectContext.Categories.Where(selector); LoadOperation&lt;Employee&gt; loader = context.Load( context.GetEmployeesQuery() .Where( selector ) ); loader.Completed += (op) =&gt; { if ( !op.HasErrors) { } }; </code></pre> <p>Or</p> <pre><code>var selector = QueryHelper.ToPredicate&lt;Category&gt;("theColumn", true); return this.ObjectContext.Categories.Where(selector); </code></pre> <p>One thing to note is the line Expression e1 = Expression.<strong>Equal</strong>( np, value ); in the function. You could extend this function to do >, &lt;, =>, etc but you would need to check that the property type you were using supported the operator.</p> <p>Hope this helps.</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