2

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

1
  • 1
    which line is 338? You're supplying the wrong number of parameters there. Commented Dec 20, 2012 at 11:32

1 Answer 1

5

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?

Sign up to request clarification or add additional context in comments.

4 Comments

thanks. What I need is do build a where clause dinamically. I have to pass a unknown numbers of predicates based on a search form. I have for example 3 field and the user might use just 2 as a criteria. Or sometimes I have 5 fields and I could use all of then. I have no ideia how to proceed.
@DimasLongo: But you'll only get one value in the Where clause itself. It would be more reasonable for you to build the equivalent of queryableData.Where(x => x.DocTypeId == idTipo && x.IdIndice == 16) for example... at which point you have one parameter, but you use property expressions to extract different bits from the entity.
Based on that would be possible to queryableData.Where(x => x.DocTypeId == idTipo && (x.IdIndice == 16 && x.IdIndice.value == 'value') ).. I have diferent idIndice.. for instance for idIndice == 16 check for value x and for idIndice == 17 check for value y
@DimasLongo: I'm afraid I don't really understand your comments - but it's not clear that you've taken on board my main point - that you should be working on the basis of a single parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.