using System.Linq.Expressions;
using System.Collections;
using System.Collections.Generic;
public static void Main()
string[] customers = { "Maria Anders", "Ana Trujillo", "Thomas Hardy", "Janine Labrune",
"Sven Ottlieb", "Diego Roel", "Eduardo Saavedra", "Patricia McKenna"};
Console.WriteLine("****************** C# Equivalent Output **************************\n\n");
.Where(customer => (customer.ToLower() == "diego roel" || customer.Length <= 12))
.OrderBy(customer => customer);
DisplayData(list.AsQueryable());
Console.WriteLine("\n\n***************** Expression Tree Output **************************\n\n");
IQueryable<String> queryableData = customers.AsQueryable<string>();
ParameterExpression pe = Expression.Parameter(typeof(string), "customer");
Expression left = Expression.Call(pe, typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));
Expression right = Expression.Constant("diego roel");
Expression e1 = Expression.Equal(left, right);
left = Expression.Property(pe, typeof(string).GetProperty("Length"));
right = Expression.Constant(12, typeof(int));
Expression e2 = Expression.LessThanOrEqual(left, right);
Expression predicateBody = Expression.OrElse(e1, e2);
MethodCallExpression whereCallExpression = Expression.Call(
new Type[] { queryableData.ElementType },
queryableData.Expression,
Expression.Lambda<Func<string, bool>>(predicateBody, new ParameterExpression[] { pe }));
MethodCallExpression orderByCallExpression = Expression.Call(
new Type[] { queryableData.ElementType, queryableData.ElementType },
Expression.Lambda<Func<string, string>>(pe, new ParameterExpression[] { pe }));
IQueryable<string> results = queryableData.Provider.CreateQuery<string>(orderByCallExpression);
private static void DisplayData(IQueryable<string> list)
foreach (var item in list)