public
Created

If your LINQ provider supports basic filtering but doesn't support Contains() (looking at you WCF Data Services), here's a workaround

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
using System;
using System.Linq;
using System.Linq.Expressions;
 
static class Program {
static Expression<Func<T, bool>> In<T, R>(this Expression<Func<T, R>> member, params R[] values) {
var prop = member.Body as MemberExpression;
if (prop == null)
throw new Exception("Expression has to be member");
if (values.Length == 0)
return _ => true;
var body = values.Select(v => Expression.Equal(prop, Expression.Constant(v))).Aggregate(Expression.OrElse);
return Expression.Lambda<Func<T, bool>>(body, member.Parameters[0]);
}
 
// Helps with type inference
static Expression<Func<A, B>> E<A, B>(Expression<Func<A, B>> exp) {
return exp;
}
 
class SomeModel {
public string Id { get; set; }
}
 
static void Main(string[] args) {
var values = new[] { "value1", "value2" };
var exp = E((SomeModel x) => x.Id).In(values);
Console.WriteLine(exp); // x => ((x.Id == "value1") OrElse x.Id == "value2"))
}
}

Nice, but one suggestion:

if (values.Length == 0)
return _ => true;

should be changed to
if (values.Length == 0)
        return _ => false;

I think it makes logical sense for the expressions to return false since the value can never be contained in an empty collection.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.