【发布时间】:2016-12-07 20:26:24
【问题描述】:
我是 Linq 的新手,也是表达式树的真正初学者。
我有一个通用表达式例程,它构建了一个简单的 Linq where 子句,我在以下位置找到了该子句:
https://www.simple-talk.com/dotnet/net-framework/dynamic-linq-queries-with-expression-trees/
public Func<TSource,bool> SimpleFilter<TSource> (string property, object value)
{
var type = typeof(TSource);
var pe = Expression.Parameter(type, "p");
var propertyReference = Expression.Property(pe,property);
var constantReference = Expression.Constant(value);
var ret = Expression.Lambda<Func<TSource, bool>>
(Expression.Equal(propertyReference, constantReference), new[] { pe });
return ret.Compile();
}
当我将函数称为SimpleFilter("JobCustomerID", 449152) 时
产生(p => p.JobCustomerId == 449152),这是正确的。
如果我在我的 Linq 语句中手动放置该条件,我会得到正确的返回。
var jj = db.VW_Job_List.Where((p => p.JobCustomerId == 449152));
但是,当通过过滤器函数调用时,Linq 会抛出一个OutOfMemoryException。
它在我的应用程序中被称为:
var jj = db.VW_Job_List.Where(SimpleFilter<VW_Job_List>("JobCustomerID", 449152));
如果我使用文本标准调用该函数,它会正确返回:
var jj = db.VW_Job_List.Where(SimpleFilter<VW_Job_List>("CompanyCode", "LCS"));
对于使用需要适应的整数变量有什么特别的要求吗?我有什么东西编码不正确吗?任何想法或见解将不胜感激。
【问题讨论】:
-
所以你查询数据库?如果是,VW_Job_List 表中有多少行?
标签: c# entity-framework linq expression-trees dynamic-linq