简体   繁体   English

如何创建表达式树以执行类似于SQL“Like”命令的操作

[英]How to create an Expression Tree to do something similar to the SQL “Like ” command

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. 它目前支持:equals,not-equals,IsNull等。我需要添加一些东西,允许它使用类似于SQL“Like”命令或使用正则表达式的通配符比较。 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. 目前,代码解析XML文件并提取数据,然后使用类似于下面所示的代码的代码处理这些数据。 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. “callExp”是一个MemberExpression,它基本上保存了我的表(Entities)的字段名称,GetConstantExpression获取了我正在比较的数据的详细信息。

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

What I'm after is a way to create an “Expression” that is similar to the “Like” command. 我所追求的是一种创建类似于“Like”命令的“Expression”的方法。 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? 有什么好的资源可以帮助这个领域吗?

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

New code based on feedback: 基于反馈的新代码:

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? 我是否朝着正确的方向创建“StartsWith”表达式? _entityExp is a ParameterExpression reference to MyClass. _entityExp是对MyClass的ParameterExpression引用。

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);

Method 'Boolean StartsWith(System.String)' declared on type 'System.String' cannot be called with instance of type 'MyClass' 在'System.String'类型上声明的方法'Boolean StartsWith(System.String)'不能使用'MyClass'类型的实例调用

Expression trees can only represent the same sort of functionality as you get in .NET languages - method calls, property evaluation etc. 表达式树只能表示与.NET语言相同的功能 - 方法调用,属性评估等。

The closest you normally get to "like" is to call string.StartsWith , string.EndsWith or string.Contains . 你通常最接近“喜欢”的是调用string.StartsWithstring.EndsWithstring.Contains If you want to deal with regular expressions instead, you might want to use Regex.IsMatch instead. 如果您想要处理正则表达式,则可能需要使用Regex.IsMatch Either way, this is something which is encapsulated in methods rather than in the "language" of expression trees itself. 无论哪种方式,这都是封装在方法中而不是表达树本身的“语言”中的东西。

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. 可以创建自己的“Like”方法,消费者会注意到并正确处理,例如......或者您可以使用现有的字符串/正则表达式方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM