在 C# 中,如何将空检查添加到动态快速代码中

发布于 2024-10-31 12:33:47 字数 1544 浏览 0 评论 0原文

我正在利用此项目使用 jqgrid 来过滤和排序集合。我看到的一个问题是,当循环遍历集合并检查字符串字段的属性是否为空时,下面的代码将会崩溃。我想调整下面的代码,以便它创建一个 lambda,它不仅执行“ToString()”和“Contains”,而且还支持属性是否为 null(并将其视为 string.empty)

我基本上有 2 个输入下面的函数:

  1. FieldName
  2. 数据值

和下面的代码动态构建一个表达式以循环遍历集合,并针对映射到传入的 FieldName 的属性检查“包含”。

我的代码如下所示:

        ParameterExpression parameter = Expression.Parameter(query.ElementType, "p");

        MemberExpression memberAccess = null;
        MethodCallExpression memberAccessToString = null;
        foreach (var property in column.Split('.'))
        {
            memberAccess = MemberExpression.Property(memberAccess ?? (parameter as Expression), property);
            memberAccessToString = MemberExpression.Call(memberAccess, "ToString", new Type[] {}, new Expression[] {});
        }

        Expression condition = null;
        LambdaExpression lambda = null;
            case WhereOperation.Contains:
                condition = Expression.Call(memberAccessToString,
                    typeof(string).GetMethod("Contains"),
                    Expression.Constant(value));
                lambda = Expression.Lambda(condition, parameter);

        MethodCallExpression result = Expression.Call(
               typeof(Queryable), "Where",
               new[] { query.ElementType },
               query.Expression,
               lambda);

        return query.Provider.CreateQuery<T>(result);

i am leveraging this project to use jqgrid to filter and sort collections. The one issue i see if that, when looping through the collection and checking the property of a string field is null, this code below will blow up. i want to tweak the code below so it creates a lambda that not only does a "ToString()" and "Contains" but also support if a property is null (and treats it as string.empty)

I basically have 2 inputs into this function below:

  1. FieldName
  2. Data Value

and the code below is dynamically building up an expression to loop through a collection and check "Contains" against the property that maps onto the FieldName being passed in.

i have code that looks like this:

        ParameterExpression parameter = Expression.Parameter(query.ElementType, "p");

        MemberExpression memberAccess = null;
        MethodCallExpression memberAccessToString = null;
        foreach (var property in column.Split('.'))
        {
            memberAccess = MemberExpression.Property(memberAccess ?? (parameter as Expression), property);
            memberAccessToString = MemberExpression.Call(memberAccess, "ToString", new Type[] {}, new Expression[] {});
        }

        Expression condition = null;
        LambdaExpression lambda = null;
            case WhereOperation.Contains:
                condition = Expression.Call(memberAccessToString,
                    typeof(string).GetMethod("Contains"),
                    Expression.Constant(value));
                lambda = Expression.Lambda(condition, parameter);

        MethodCallExpression result = Expression.Call(
               typeof(Queryable), "Where",
               new[] { query.ElementType },
               query.Expression,
               lambda);

        return query.Provider.CreateQuery<T>(result);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2024-11-07 12:33:47

您是否检查过Expression.Coalesce,您可以在调用ToString()之前将其应用到memberAccess

memberAccess = MemberExpression.Property(memberAccess ?? (parameter as Expression), property);
memberAccessToString = MemberExpression.Call(Expression.Coalesce(memberAccess,Expression.Constant(string.Empty)), "ToString", new Type new Type[] {}, new Expression[] {});

没有测试它,但可能会导致解决方案

Have you checked Expression.Coalesce you could apply it to memberAccess before calling ToString()

memberAccess = MemberExpression.Property(memberAccess ?? (parameter as Expression), property);
memberAccessToString = MemberExpression.Call(Expression.Coalesce(memberAccess,Expression.Constant(string.Empty)), "ToString", new Type new Type[] {}, new Expression[] {});

Didn't test it but may lead to the solution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文