I have expression
Expression<Func<Car, Driver, bool>> CanBeDrivenBy =
(car, driver) => car.Category == 'B' && driver.Age > 18;
and I want to get cars which can be driven by some driver
IQueryable<Cars> cars = ...;
Driver driver = ...;
cars.Where(CanBeDrivenBy); // Fail, expecting Expression<Func<Car, bool>>
So I need to convert Expression<Func<Car, Driver, bool>>
to Expression<Func<Car, bool>>
(specify driver)
Yes I can use
cars.Where(c => c.Category == 'B' && driver.Age > 18);
but I need solution with expression which can be changed dynamicly. And I need to pass Expression (using entity framework)
This WORKS
I wrote this function to reduce number of arguments from 2 to 1 by specifying the second argument.
public static Expression<Func<T1, TResult>> Bind2nd<T1, T2, TResult>(Expression<Func<T1, T2, TResult>> source, T2 argument)
{
Expression arg2 = Expression.Constant(argument, typeof(T2));
var arg1 = Expression.Parameter(typeof(T1));
return Expression.Lambda<Func<T1, TResult>>(Expression.Invoke(source, arg1, arg2), arg1);
}
Usage:
IQueryable<Car> cars = ...;
Driver driver = ...;
cars.Where(Bind2nd(CanBeDrivenBy, driver));
arg1
is temporary storage between calls.
Is there any system equivalent function?