System.ArgumentException: Incorrect number of parameters supplied for lambda declaration in System.Linq.Expressions method

.net c# expression-trees linq linq-to-sql

Question

Mates, I am trying to build a expression tree using System.Linq.Expressions and I am getting this error:

Erro: System.ArgumentException: Incorrect number of parameters supplied for lambda declaration at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, Boolean tailCall, IEnumerable`1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, ParameterExpression[] parameters) at Gedi.Controllers.OperacaoController.opBuscaFile(FormCollection form) in c:\Users\Guilherme\Documents\Visual Studio 2012\Projects\Gedi\Gedi\Controllers\OperacaoController.cs:line 338

The code:

IQueryable<String> queryableData = AllIndexValues.AsQueryable<string>();


//docTypeId == idTipo
ParameterExpression pe1 = Expression.Parameter(typeof(int), "docTypeId");
Expression right = Expression.Constant(idTipo);
Expression e1 = Expression.Equal(pe1, right);


//idIndice == 16
ParameterExpression pe2 = Expression.Parameter(typeof(int), "idIndice");
right = Expression.Constant(16, typeof(int));
Expression e2 = Expression.Equal(pe2, right);

//docTypeId == idTipo AND idIndice == 16
Expression predicateBody = Expression.And(e1,e2);

//queryableData.Where(docTypeId => (docTypeId ==  idTipo) AND idIndice => (idIndice ==  16)) 
MethodCallExpression whereCallExpression = Expression.Call(typeof(Queryable), "Where", new Type[] { queryableData.ElementType }, queryableData.Expression, Expression.Lambda<Func<string, bool>>(predicateBody, new ParameterExpression[] { pe1, pe2 }));

IQueryable<string> results = queryableData.Provider.CreateQuery<string (whereCallExpression);

return Content(""+results);

I have adapted this code from here http://msdn.microsoft.com/en-us/library/vstudio/bb882637.aspx

Thanks

Accepted Answer

This is the initial problem, I think:

Expression.Lambda<Func<string, bool>>(predicateBody,
                                      new ParameterExpression[] { pe1, pe2 }))

A Func<string, bool> takes just a string and returns a bool. So it only has a single parameter. You're passing in two ParameterExpressions. Also, they're both int parameters... not a string in sight!

So you could use:

Expression.Lambda<Func<int, int, bool>>(predicateBody,
                                        new ParameterExpression[] { pe1, pe2 }))

... but my guess is that's not going to help you, if you want a Where clause...

Given this comment:

//queryableData.Where(docTypeId => (docTypeId ==  idTipo) AND idIndice => (idIndice ==  16)) 

... it sounds like you're confused even before we get to expression trees. You can't combine two lambda expressions like that.

I strongly advise you to work out what your code would look like if you didn't need to build expression trees, and then convert it. What is the element type of queryableData? You're only going to get one value per predicate test - is that going to be a docTypeId or an idIndice?



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why