2012-12-20 25 views
1

配合拉姆達聲明提供的參數錯誤的數字,我想用System.Linq.Expressions建立一個表達式樹,我得到這個錯誤:System.ArgumentException:對在System.Linq.Expressions方法

Erro: System.ArgumentException: Incorrect number of parameters supplied for lambda declaration at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection 1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable 1 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

代碼:

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); 

我從這裏適應這個代碼http://msdn.microsoft.com/en-us/library/vstudio/bb882637.aspx

臨屋NKS

+1

哪一行是338?您在那裏提供了錯誤的參數數量。 – Jodrell

回答

4

這是最初的問題,我認爲:

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

一個Func<string, bool>只需一個string並返回一個bool。所以它只有一個參數。您正在通過兩個ParameterExpressions。此外,他們int參數...不是一個字符串在視線內!

所以,你可以使用:

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

...但我的猜測是,這不會幫助你,如果你想有一個Where條款等

鑑於此評論:

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

......聽起來好像你在表達樹之前感到困惑。你不能像這樣組合兩個lambda表達式。

我強烈建議你制定一下你的代碼的樣子,如果你沒有需要構建表達式樹,然後將其轉換。什麼是queryableData的元素類型?每個謂詞測試只會得到一個值 - 這是一個docTypeId還是一個idIndice

+0

謝謝。我需要的是建立一個地方條款dinamically。我必須根據搜索表單傳遞未知數量的謂詞。例如,我有3個字段,用戶可能只使用2作爲標準。或者有時我有5個領域,我可以全部使用。我不知道如何繼續。 –

+0

@DimasLongo:但是你只會在where子句本身中得到一個*值。例如,構建'queryableData.Where(x => x.DocTypeId == idTipo && x.IdIndice == 16)'等價物將會更加合理......在這一點上,您有一個*參數,但是您使用屬性表達式從實體中提取不同的位。 –

+0

基於那將是可能的queryableData.Where(x => x.DocTypeId == idTipo &&(x.IdIndice == 16 && x.IdIndice.value =='value'))..我有不同的idIndice ..例如對於idIndice == 16檢查值x和idIndice == 17檢查值y –

相關問題