c# - LINQ 表达式。从范围引用的类型变量 'p',但未定义

标签 c# linq expression-trees

我正在使用这段代码动态构建 LINQ 查询。 它似乎有效,但是当我在搜索中有多个 searchString 时,(所以当添加多个表达式时,我得到以下错误:

Variable 'p' of type referenced from scope, but it is not defined**

我想我只能定义/使用 p 一次。但是,如果是这样,我需要稍微修改一下我的代码。谁能在这里指出我正确的方向?

    if (searchStrings != null)
    {
        foreach (string searchString in searchStrings)
        {
            Expression<Func<Product, bool>> containsExpression = p => p.Name.Contains(searchString);
            filterExpressions.Add(containsExpression);
        }
    }

    Func<Expression, Expression, BinaryExpression>[] operators = new Func<Expression, Expression, BinaryExpression>[] { Expression.AndAlso };
    Expression<Func<Product, bool>> filters = this.CombinePredicates<Product>(filterExpressions, operators);

    IQueryable<Product> query = cachedProductList.AsQueryable().Where(filters);

    query.Take(itemLimit).ToList();  << **error when the query executes**


    public Expression<Func<T, bool>> CombinePredicates<T>(IList<Expression<Func<T, bool>>> predicateExpressions, Func<Expression, Expression, BinaryExpression> logicalFunction)
    {
        Expression<Func<T, bool>> filter = null;

        if (predicateExpressions.Count > 0)
        {
            Expression<Func<T, bool>> firstPredicate = predicateExpressions[0];
            Expression body = firstPredicate.Body;
            for (int i = 1; i < predicateExpressions.Count; i++)
            {
                body = logicalFunction(body, predicateExpressions[i].Body);
            }
            filter = Expression.Lambda<Func<T, bool>>(body, firstPredicate.Parameters);
        }

        return filter;
    }

最佳答案

简化,这里有几行你正在尝试做(我使用字符串而不是产品等,但想法是一样的):

        Expression<Func<string, bool>> c1 = x => x.Contains("111");
        Expression<Func<string, bool>> c2 = y => y.Contains("222");
        var sum = Expression.AndAlso(c1.Body, c2.Body);
        var sumExpr = Expression.Lambda(sum, c1.Parameters);
        sumExpr.Compile(); // exception here

请注意我如何将您的 foreach 扩展为两个带有 x 和 y 的表达式 - 这正是编译器看起来的样子,它们是不同参数。

换句话说,你正在尝试做这样的事情:

x => x.Contains("...") && y.Contains("...");

编译器想知道“y”变量是什么?

要修复它,我们需要为所有表达式使用完全相同的参数(不仅是名称,还有引用)。我们可以像这样修复这个简化的代码:

        Expression<Func<string, bool>> c1 = x => x.Contains("111");
        Expression<Func<string, bool>> c2 = y => y.Contains("222");
        var sum = Expression.AndAlso(c1.Body, Expression.Invoke(c2, c1.Parameters[0])); // here is the magic
        var sumExpr = Expression.Lambda(sum, c1.Parameters);
        sumExpr.Compile(); //ok

因此,修复原始代码就像:

internal static class Program
{
    public class Product
    {
        public string Name;
    }

    private static void Main(string[] args)
    {
        var searchStrings = new[] { "111", "222" };
        var cachedProductList = new List<Product>
        {
            new Product{Name = "111 should not match"},
            new Product{Name = "222 should not match"},
            new Product{Name = "111 222 should match"},
        };

        var filterExpressions = new List<Expression<Func<Product, bool>>>();
        foreach (string searchString in searchStrings)
        {
            Expression<Func<Product, bool>> containsExpression = x => x.Name.Contains(searchString); // NOT GOOD
            filterExpressions.Add(containsExpression);
        }

        var filters = CombinePredicates<Product>(filterExpressions, Expression.AndAlso);

        var query = cachedProductList.AsQueryable().Where(filters);

        var list = query.Take(10).ToList();
        foreach (var product in list)
        {
            Console.WriteLine(product.Name);
        }
    }

    public static Expression<Func<T, bool>> CombinePredicates<T>(IList<Expression<Func<T, bool>>> predicateExpressions, Func<Expression, Expression, BinaryExpression> logicalFunction)
    {
        Expression<Func<T, bool>> filter = null;

        if (predicateExpressions.Count > 0)
        {
            var firstPredicate = predicateExpressions[0];
            Expression body = firstPredicate.Body;
            for (int i = 1; i < predicateExpressions.Count; i++)
            {
                body = logicalFunction(body, Expression.Invoke(predicateExpressions[i], firstPredicate.Parameters));
            }
            filter = Expression.Lambda<Func<T, bool>>(body, firstPredicate.Parameters);
        }

        return filter;
    }
}

但请注意输出:

222 should not match
111 222 should match

不是你可能期望的。这是在 foreach 中使用 searchString 的结果,应该按以下方式重写:

        ...
        foreach (string searchString in searchStrings)
        {
            var name = searchString;
            Expression<Func<Product, bool>> containsExpression = x => x.Name.Contains(name);
            filterExpressions.Add(containsExpression);
        }
        ...

这是输出:

111 222 should match

关于c# - LINQ 表达式。从范围引用的类型变量 'p',但未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15589239/

相关文章:

c# - 动态方法、表达式树和 DLR

c# - 比较两个列表中对象的 id,并获取包含 id 出现在两个列表中的对象的对象列表

c# - 确定元素在 IQueryable 中的位置

c# - LINQ 项目总和集合以返回带有结果的对象(多列)

c++ - 将前缀表达式树 vector 转换为 ORF/Karva 表示法表达式树 vector

c# - WinBioCaptureSample 返回哪些指纹格式?

c# - 使用 NHibernate 和 CaSTLe WCF 工具在 WCF 上下文中处理 Rebus 消息

c# - 无法在 SlidingUpPanelLayout Xamarin Android 中使用 ScrollView 布局

c# - 如何以编程方式解析和修改 C# 代码

c# - 如何获得影响其输出的表达式树的所有因素