Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To take the example literally, you could perform the combination in the question by building an <code>Expression</code> on the fly:</p> <pre><code> static IQueryable&lt;T&gt; WhereXElseY&lt;T&gt;( this IQueryable&lt;T&gt; query, Expression&lt;Func&lt;T, bool&gt;&gt; predicateA, Expression&lt;Func&lt;T, bool&gt;&gt; predicateB) { var condA = predicateA.Body; var param = predicateA.Parameters[0]; var body = Expression.OrElse( condA, Expression.AndAlso( Expression.Not(condA), Expression.Invoke( predicateB, param))); var lambda = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( body, param); return query.Where(lambda); } </code></pre> <p>However, while this may work with LINQ-to-SQL, it won't work with EF since EF sadly hates <code>Expression.Invoke</code>. But as Jon notes; if you are sending this down to a database backend, the priority is irrelevant and you might as well go with the logically equivalent <code>condition1 || condition2</code>. You can combine expressions something like:</p> <pre><code> static IQueryable&lt;T&gt; WhereAny&lt;T&gt;( this IQueryable&lt;T&gt; query, params Expression&lt;Func&lt;T, bool&gt;&gt;[] predicates) { if (predicates == null) throw new ArgumentNullException("predicates"); if (predicates.Length == 0) return query.Where(x =&gt; false); if (predicates.Length == 1) return query.Where(predicates[0]); var param = predicates[0].Parameters[0]; var body = predicates[0].Body; for (int i = 1; i &lt; predicates.Length; i++) { body = Expression.OrElse( body, Expression.Invoke(predicates[i], param)); } var lambda = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(body, param); return query.Where(lambda); } </code></pre> <p>If I've missed the point, please clarify...</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