I have two expressions that are built out at separate times, but need to be merged in order to get an accurate 'grouping' of a where clause. I did try this option, but I am using Entity Framework and it doesn't understand the Invoke
function. I have seen some of the ExpressionVisitor
alternative but I do not think I have a good enough understanding of what I need to do.
If anyone could please point me in the right direction I would much appreciate it, it feels like it is close to there.
Where Clause 1A (object type Expression<Func<T, bool>>
)
{parm => parm.Name.Contains("hat")}
Where Clause 1B (object type LambdaExpression
, but can use (Expression<Func<T, bool>>)LambdaExpression
)
{parm => parm.Attributes.Any(parm => ((parm.Name == "test") AndAlso (parm.Value == "21")))}
Needed Where Clause
{parm =>
parm.Name.Contains("hat") (&&/||)
parm.Attributes.Any(parm => ((parm.Name == "test") AndAlso (parm.Value == "21")))
}
If someone could please help me merge Where Clause 1A and Where Clause 1B, I would be very thankful..
Just an FYI Where() and Any() clause are generic methods obtained from IQueryable, not sure if that matters.
Func<MethodInfo, bool> methodLambda = m => m.Name == "Any" && m.GetParameters().Length == 2;
MethodInfo method = typeof(Queryable).GetMethods().Where(methodLambda).Single().MakeGenericMethod(ActualQueryableType);
ParameterExpression parentMember = Expression.Parameter(typeof(T), "parentParm");
// Create Any() Expression tree and convert it to lambda
MethodCallExpression callAny = Expression.Call(method, member, exp);
LambdaExpression lambdaAny = Expression.Lambda(callAny, param);
var combExp = parentExp.And((Expression<Func<T, bool>>)lambdaAny);
MethodCallExpression whereCall = Expression.Call(typeof(Queryable), "Where", new Type[] { query.ElementType }, new Expression[] {
query.Expression,
Expression.Quote(combExp)
});
query = (IQueryable<T>)query.Provider.CreateQuery(whereCall);
The error I get when using Invoke
is:
NotSupportedException
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
Invoke
you get?Invoke
isn't supported. Why would the specific type of exception be relevant?Expression.Invoke
and I'm wondering if OP isn't usingFunc.Invoke
instead, which I'm pretty sure would fail.