How to write like statements in Entity Framework when the liked valued contains %

…衆ロ難τιáo~ 提交于 2020-01-16 04:55:08

问题


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?


回答1:


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




回答2:


You can create a Stored procedure and execute it using entity framework.



来源:https://stackoverflow.com/questions/7202217/how-to-write-like-statements-in-entity-framework-when-the-liked-valued-contains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!