Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The use with generics is problematic, since C# doesn't support operators on generics - meaning you'd have to write the expression manually. And as we've already seen, string works differently. But for the rest, how about something like (untested):</p> <p>(<strong>edited</strong> for multiple ranges)</p> <pre><code> public static IQueryable&lt;TSource&gt; WhereBetween&lt;TSource, TValue&gt;( this IQueryable&lt;TSource&gt; source, Expression&lt;Func&lt;TSource, TValue&gt;&gt; selector, params Range&lt;TValue&gt;[] ranges) { return WhereBetween&lt;TSource,TValue&gt;(source, selector, (IEnumerable&lt;Range&lt;TValue&gt;&gt;) ranges); } public static IQueryable&lt;TSource&gt; WhereBetween&lt;TSource, TValue&gt;( this IQueryable&lt;TSource&gt; source, Expression&lt;Func&lt;TSource, TValue&gt;&gt; selector, IEnumerable&lt;Range&lt;TValue&gt;&gt; ranges) { var param = Expression.Parameter(typeof(TSource), "x"); var member = Expression.Invoke(selector, param); Expression body = null; foreach(var range in ranges) { var filter = Expression.AndAlso( Expression.GreaterThanOrEqual(member, Expression.Constant(range.A, typeof(TValue))), Expression.LessThanOrEqual(member, Expression.Constant(range.B, typeof(TValue)))); body = body == null ? filter : Expression.OrElse(body, filter); } return body == null ? source : source.Where( Expression.Lambda&lt;Func&lt;TSource, bool&gt;&gt;(body, param)); } </code></pre> <p>Note; the use of Expression.Invoke means it will probably work on LINQ-to-SQL but not EF (at the moment; hopefully fixed in 4.0).</p> <p>With usage (tested on Northwind):</p> <pre><code>Range&lt;decimal?&gt; range1 = new Range&lt;decimal?&gt;(0,10), range2 = new Range&lt;decimal?&gt;(15,20); var qry = ctx.Orders.WhereBetween(order =&gt; order.Freight, range1, range2); </code></pre> <p>Generating TSQL (re-formatted):</p> <pre><code>SELECT -- (SNIP) FROM [dbo].[Orders] AS [t0] WHERE (([t0].[Freight] &gt;= @p0) AND ([t0].[Freight] &lt;= @p1)) OR (([t0].[Freight] &gt;= @p2) AND ([t0].[Freight] &lt;= @p3)) </code></pre> <p>Just what we wanted ;-p</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