如果在实体框架中写入类似语句的话,那么当值被包含%

问题描述:

我知道 String.Contains('x')将转换为 LIKE'%x%' String.StartsWith('x') String.EndsWith('x')将转换为 LIKE'%x' LIKE'x%'但是,我现在处于业务层中要构建LIKE运算符正则表达式子句的情况,并通过实体框架将其发送到SQL Server。我的意思是,现在最终用户通过GUI构建正则表达式,我们需要根据这个获取结果。例如,用户创建一个类似于 [Any Letter] [Any Number] ab [Many Characters] 的东西,我们把它们翻译成 [a-zA-Z ] [0-9] AB%。现在我们要使用实体框架将其发送到SQL Server。然而,似乎实体框架无法做到这一点,查询将无法得到正确的结果。

I know that String.Contains('x') would translate to LIKE '%x%', and String.StartsWith('x') and String.EndsWith('x') would translate into LIKE '%x' and LIKE 'x%' respectively. However, I'm now in a situation in which I'm gonna build the LIKE operator regular expression clause in the business layer, and send it to the SQL Server through Entity Framework. I mean, now the end users builds the regular expression through a GUI and we need to retrieve the result based on that. For example, user creates something like [Any Letter][Any Number]ab[Many Characters] and we translate it into [a-zA-Z][0-9]ab%. Now we want to send this to SQL Server using Entity Framework. However, it seems that Entity Framework can't do this, and the query won't get the correct result back.

任何想法我们应该如何实现这个要求? / p>

Any idea how we should implement this requirement?

实体框架能够做到这一点,但是linq-to-entities不是。您必须使用实体SQL LIKE 操作符。 一些示例

Entity framework is able to do this but linq-to-entities are not. You must use Entity SQL and LIKE operator. Some example.