Skip to content

@mausch /gist:6893533

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
If your LINQ provider supports basic filtering but doesn't support Contains() (looking at you WCF Data Services), here's a workaround
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"))

Equivalent F# :


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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.