using System.Linq.Expressions;
public static void Main()
Console.WriteLine("Hello World");
private static IQueryable<Data> GetData()
private static string[] Codes = new string[] { "ALPHA", "BETA" };
private static IQueryable<Data> BaseQuery()
IQueryable<Data> query = GetData();
Expression<Func<Data, bool>> expression = x=>false;
foreach (var row in Codes)
expression = expression.Or(t => t.Comp_Code == row.ToString());
return query.Where(expression);
public string Comp_Code { get; set; }
public static class ExpressionExt
private class ParameterReplaceVisitor : ExpressionVisitor
public ParameterExpression Target { get; set; }
public ParameterExpression Replacement { get; set; }
protected override Expression VisitParameter(ParameterExpression node)
return node == Target ? Replacement : base.VisitParameter(node);
public static Expression<Func<T, bool>> Or<T>(
this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
var visitor = new ParameterReplaceVisitor()
Target = right.Parameters[0],
Replacement = left.Parameters[0],
var rewrittenRight = visitor.Visit(right.Body);
var andExpression = Expression.OrElse(left.Body, rewrittenRight);
return Expression.Lambda<Func<T, bool>>(andExpression, left.Parameters);
public static Expression<Func<T, bool>> And<T>(
this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
var visitor = new ParameterReplaceVisitor()
Target = right.Parameters[0],
Replacement = left.Parameters[0],
var rewrittenRight = visitor.Visit(right.Body);
var andExpression = Expression.OrElse(left.Body, rewrittenRight);
return Expression.Lambda<Func<T, bool>>(andExpression, left.Parameters);