问题
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