//Query = c.PurchaseDate == new DateTime(2011, 11, 29) && c.Name
// == "Elizabeth Brown" )
IQueryable<Customer> customers = _customers.AsQueryable<Customer>();
//Predicate parameter
ParameterExpression parameter = Expression.Parameter(typeof(Customer),
"customer");
//Create left expression
Expression left = Expression.Property(parameter, typeof(Customer)
.GetProperty("PurchaseDate"));
Expression right = Expression.Constant(new DateTime(2011, 11, 29));
Expression leftExp = Expression.Equal(left, right);
//Create right expression tree
left = Expression.Property(parameter, typeof(Customer).GetProperty("Name"));
right = Expression.Constant("Elizabeth Brown", typeof(string));
Expression rightExp = Expression.Equal(left, right);
//Combine the expressions into expression tree
Expression expressionTree = Expression.AndAlso(leftExp, rightExp);
//Create an expression tree that represents the expression
MethodCallExpression methodCall = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { customers.ElementType },
customers.Expression,
Expression
.Lambda<Func<Customer, bool>>
(expressionTree, new ParameterExpression[] { parameter }));
// Create an executable query from the expression tree.
IQueryable<Customer> results =
customers.Provider.CreateQuery<Customer>(methodCall);
// Enumerate the results
foreach (Customer customer in results)
{
Console.WriteLine("{0} {1}", customer.Name, customer.PurchaseDate);
}
Console.ReadLine();
Я закончил задачу таким образом.IQueryable действительно замечательный материал.Наслаждайтесь! * * 1002