在 C# 中,如何将空检查添加到动态快速代码中
我正在利用此项目使用 jqgrid 来过滤和排序集合。我看到的一个问题是,当循环遍历集合并检查字符串字段的属性是否为空时,下面的代码将会崩溃。我想调整下面的代码,以便它创建一个 lambda,它不仅执行“ToString()”和“Contains”,而且还支持属性是否为 null(并将其视为 string.empty)
我基本上有 2 个输入下面的函数:
- FieldName
- 数据值
和下面的代码动态构建一个表达式以循环遍历集合,并针对映射到传入的 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:
- FieldName
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否检查过
Expression.Coalesce
,您可以在调用ToString()之前将其应用到memberAccess没有测试它,但可能会导致解决方案
Have you checked
Expression.Coalesce
you could apply it to memberAccess before calling ToString()Didn't test it but may lead to the solution