伙计们,我正在尝试使用 System.Linq.Expressions 构建表达式树,但出现此错误:
错误:System.ArgumentException:为 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 参数)在 System.Linq.Expressions.Expression.Lambda[TDelegate](Expression)的 lambda 声明提供的参数数量不正确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:第 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
谢谢