I am trying to build an Entity Framework 4 Many to Many relationship filter, this is for a Dynamic Data project. I know that I need to build an Expression Tree at run time, and I am familiar with doing this for something like a simple where expression like this:
private MethodCallExpression BuiltMethodCall(IQueryable _query, Type _ObjType, string _ColumnToSearch, string _SearchValue)
{
ConstantExpression value = Expression.Constant(_SearchValue);
ParameterExpression _parameter = Expression.Parameter(_ObjType, "value");
MemberExpression _property = Expression.Property(_parameter, _ColumnToSearch);
BinaryExpression comparison = Expression.Equal(_property, value);
LambdaExpression lambda = Expression.Lambda(comparison, _parameter);
//Ex: Customers.Select(c => c).Where(value => (value.City == "Seattle"))
MethodCallExpression _where = Expression.Call(typeof(Queryable), "Where", new Type[] { _query.ElementType }, new Expression[] {
_query.Expression,
Expression.Quote(lambda)
});
return _where;
}
Just for simplicity, these examples use the Northwind database where there is a many to many join of (Customers <- CustomerCustomerDemo -> CustomerDemographics). What I am having trouble with is building the expression tree when there is a nested lambda like in the expression below, where I am retrieving all customers who have a specific customer demographic.
string custDemogID = "3";
//Get customers who have a particular demog
dynamic cust = context.Customers.Where(c => c.CustomerDemographics.Any(cd => cd.CustomerTypeID == custDemogID));
How do I build the Expression tree having a "where" call, that includes the nested lambda for the "Any"? Any help is appreciated, even if it is pseudo code on how to do it. There is not much out the on this, I am desperate, please help!