c# - System.ArgumentException : Incorrect number of parameters supplied for lambda declaration in System. Linq.Expressions 方法

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

friend 们,我正在尝试使用 System.Linq.Expressions 构建表达式树,但收到此错误:

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

代码:

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

谢谢

最佳答案

我认为这是最初的问题:

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 的元素类型是什么?每个谓词测试您只会得到一个值 - 是 docTypeIdidIndice

关于c# - System.ArgumentException : Incorrect number of parameters supplied for lambda declaration in System. Linq.Expressions 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13971229/

相关文章:

c# - 无需 TFS 或 VSTS 自动部署 Service Fabric 项目

.net - 不同提供者的 Entity Framework 使用模型

c# - 比较两个通用列表差异的最快方法

c# - 如何使用 LINQ 在 C# 中获取两个列表的差异

c# - 子串匹配索引

c# - 如何计算某个项目在列中存在的次数?

c# SQL Compact 监控 FileSystemWatcher 或 SqlDependency

c# - 将 BigInteger 转换为十进制(基数 10)字符串的最快方法?

c# - 在 ASP.Net MVC 3 中绘制图表

.net - 带有 TransferMode.Streamed 的 WCF NetTcpBinding 在 Windows Server 2019 上无法使用 TLS 1.2 和 SslProtocols.None 在 WCF 绑定(bind)上工作