Linq 动态表达式生成错误
关于这个问题,我搜索了很多,这里有什么错Linq 动态表达式生成错误,linq,c#-4.0,lambda,Linq,C# 4.0,Lambda,关于这个问题,我搜索了很多,这里有什么错 Shorter version of my code is /// companyid is integer type value here is 220 var cond1 = BuildExpression(companyId); var acntlst=entities.Accounts.Where(cond).ToList(); Account是一个查询帐户集合的类 构建表达式函数 private static Expres
Shorter version of my code is
/// companyid is integer type value here is 220
var cond1 = BuildExpression(companyId);
var acntlst=entities.Accounts.Where(cond).ToList();
Account是一个查询帐户集合的类
构建表达式函数
private static Expression<Func<Account, bool>> BuildExpression(string companyid)
{
var paramexp = Expression.Parameter(typeof (Account), "p");
var proprty = typeof(Account).GetProperty("CompanyId");
var prpexp = Expression.Property(paramexp, proprty);
var varexp = Expression.Variable(typeof(Int32), companyid);
var cond1 = Expression.Equal(prpexp, varexp);
return Expression.Lambda<Func<Account, bool>>(cond1,paramexp);
}
这将创建一个名为的变量“220”
您从未声明此变量或为其赋值 相反,您需要的是
Expression.Constant
,它接受一个值并返回一个具有该值的表达式。(您可能需要将字符串解析为
int
)
然而,您根本不需要手工构建它
相反,您应该只编写返回a=>a.CompanyId==CompanyId
这将创建一个名为的变量“220”
您从未声明此变量或为其赋值 相反,您需要的是
Expression.Constant
,它接受一个值并返回一个具有该值的表达式。(您可能需要将字符串解析为
int
)
然而,您根本不需要手工构建它
相反,您应该只编写返回a=>a.CompanyId==CompanyId
The parameter '220' was not bound in the specified LINQ to Entities query expression
Expression.Variable(typeof(Int32), companyid);