【发布时间】:2015-05-14 05:48:43
【问题描述】:
我有一个集合List<List<object>>,我需要根据List<object> 集合包含给定元素的情况过滤掉它。我能够构建 where 子句,但出现以下异常:
System.Core.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 附加信息:从范围“”引用的“System.Collections.Generic.List`1[System.Object]”类型的变量“x”,但未定义
我发现了类似的问题,并且我知道问题出在哪里,但我需要帮助来寻找解决方案。
这是我的代码:
protected override Expression<Func<List<object>, bool>> GetWhereClause()
{
var type = typeof(List<object>);
var parameterExpression = Expression.Parameter(type, "x");
Expression expressionBody = null;
if (Verified)
{
Expression<Func<List<object>, bool>> expression = x => x.Contains("Verified");
expressionBody = expression.Body;
}
if (GoodMatch)
{
Expression<Func<List<object>, bool>> expression = x => x.Contains("Good Match");
if (expressionBody != null)
expressionBody = Expression.Or(expressionBody, expression.Body);
else
expressionBody = expression.Body;
}
//More conditions here
if (expressionBody != null)
{
var whereClauseExp = Expression.Lambda<Func<List<object>, bool>>(expressionBody, parameterExpression);
return whereClauseExp;
}
return null;
}
现在,此方法生成所需的 where 子句,但是当我尝试应用它时,我得到了提到的异常。
if (whereClause != null)
{
items = items.Where(whereClause.Compile());
//
}
【问题讨论】:
-
你也可以提供堆栈跟踪吗?
标签: c# linq expression-trees