Ok so here's the problem I am creating an expression dynamically to execute against the database, that works fine. the problem appears when I add another expression using Expression.AndAlso, the expressions combine fine but the parameters don't work, my code is below.
Expression<Func<Invoice, bool>> condition = null;
ParameterExpression[] param = new ParameterExpression[sessionModel.FilterChildren.Count];
foreach (var filter in sessionModel.FilterChildren) {
param[i] = Expression.Parameter(typeof(Invoice), filter.SysName);
Type type = Type.GetType(filter.Type);
if (i == 0)
condition =
Expression.Lambda<Func<Invoice, bool>>(
Expression.Equal(
Expression.Property(param[i], filter.SysName),
Expression.Constant(filter.Value, type)
),
param[i]);
else {
var newCond = Expression.Lambda<Func<Invoice, bool>>(
Expression.Equal(
Expression.Property(param[i], filter.SysName),
Expression.Constant(filter.Value, type)
),
param[i]);
var test = Expression.AndAlso(condition.Body, newCond.Body);
condition = Expression.Lambda<Func<Invoice, bool>>(test, param);
}
i++;
}
it throws an error
Incorrect number of parameters supplied for lambda declaration
there are different parameters for each expression that will be added.
any ideas where i am going wrong?
Thanks
conditionbeforeifblock?Expression<Func<Invoice, bool>> condition = null;and then if if block is not triggeredvar test = Expression.AndAlso(condition.Body, newCond.Body);but I guessiis initially 0 as well, so that's not the case. Which line throws the exception?AndAlsopart seems to be Ok to my inexperienced eyes tbh.