如何创建表达式树以执行类似于SQL"Like"的操作命令 [英] How to create an Expression Tree to do something similar to the SQL "Like " command

查看:108
本文介绍了如何创建表达式树以执行类似于SQL"Like"的操作命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究某位同事编写的一些表达式树代码,并正在考虑添加其他表达式的可能性.它当前支持:等于,不等于,IsNull等.我需要添加一些内容,使其能够使用类似于SQL"Like"命令的通配符比较或使用正则表达式.目前,该代码解析XML文件并提取数据,然后使用类似于下面所示代码的代码对其进行处理.这是等于"表达式的一个示例. "callExp"是一个MemberExpression,它基本上保存我的表(实体)的字段名称,而GetConstantExpression获取有关我正在比较的数据的详细信息.

I’m working on some expression tree code written by a colleague and am looking into the possibility of adding additional expressions. It currently supports: equals, not-equals, IsNull etc. I need to add something that will allow it to use a wildcard comparison similar to the SQL "Like" command or using regular expressions. At the moment the code parses an XML file and extracts the data which is then processed using code similar to the line shown below. This is an example of the "Equal" expression. "callExp" is a MemberExpression that basically holds the field name of my table (Entities) and GetConstantExpression gets details about the data I am comparing.

xRet = Expression.MakeBinary(ExpressionType.Equal, callExp, GetConstantExpression(element.Element("Value"), callExp.Type));

我追求的是一种创建类似于"Like"命令的"Expression"的方法.可以使用与上述类似的几行代码来完成此操作,或者这会变得更加复杂吗?有什么好的资源可以在这方面有所帮助吗?

What I’m after is a way to create an "Expression" that is similar to the "Like" command. Can this be done using a few lines similar to above or is this going to be more complex? Any good resources that could help in this area?

================================================ ==================================

==================================================================================

基于反馈的新代码:

我正在查看一些示例,并尝试了以下希望创建一个表达式的示例.它给了我下面显示的错误.我是否朝着正确的方向创建"StartsWith"表达式? _entityExp是对MyClass的ParameterExpression引用.

I was looking at some examples and tried the following which I was hoping would create me an Expression. It gives me the error shown below. Am I going in the right direction to create a "StartsWith" expression? _entityExp is a ParameterExpression reference to MyClass.

ParameterExpression p = Expression.Parameter(_entityExp.Type, "entity");
MethodInfo method = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
var containsMethodExp = Expression.Call(p, method, Expression.Constant("root"), p);

类型为'MyClass'的实例不能调用在类型为'System.String'上声明的方法'Boolean StartsWith(System.String)'

Method 'Boolean StartsWith(System.String)' declared on type 'System.String' cannot be called with instance of type 'MyClass'

推荐答案

表达式树只能代表与您使用.NET语言所获得的功能相同的功能-方法调用,属性评估等.

Expression trees can only represent the same sort of functionality as you get in .NET languages - method calls, property evaluation etc.

通常最接近"like"的是调用string.StartsWithstring.EndsWithstring.Contains.如果要处理正则表达式,则可能要使用Regex.IsMatch.无论哪种方式,这都是封装在方法中的东西,而不是封装在表达式树本身的语言"中的东西.

The closest you normally get to "like" is to call string.StartsWith, string.EndsWith or string.Contains. If you want to deal with regular expressions instead, you might want to use Regex.IsMatch instead. Either way, this is something which is encapsulated in methods rather than in the "language" of expression trees itself.

在不了解更多如何使用表达式树的情况下,很难确切地说出您应该怎么做.您可以创建自己的"Like"方法,例如消费者可以注意到并适当处理的"Like"方法,或者可以使用现有的string/regex方法.

Without knowing more about how your expression trees are consumed, it's hard to say exactly what you should do. You could create your own "Like" method which the consumer would notice and handle appropriately, for example... or you could use the existing string/regex methods.

这篇关于如何创建表达式树以执行类似于SQL"Like"的操作命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆