当喜欢的值包含 % 时,如何在实体框架中编写 like 语句
我知道 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.
Any idea how we should implement this requirement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实体框架能够做到这一点,但 linq-to-entities 却不能。您必须使用 Entity 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.
您可以创建存储过程并使用实体框架执行它。
You can create a Stored procedure and execute it using entity framework.