I have a simple database function, which expects two strings as parameter and returns a string. I want to map this with entity framework. Similar to this question, I've created a simple function header:
[DbFunction("dbo", "StripCharacters")]
public static string StripCharacters(string input, string pattern = "^A-Z0-9") => throw new NotSupportedException();
Like in the linked post, I get the same error message, as soon as I try to use this method inside one of my queries. The exception message is:
Method
System.String StripCharacters(System.String, System.String)in typeDE.ZA.TrailerLoadingAssistant.Web.Models.DatabaseEntitiescannot be translated into a LINQ to Entities store expression
await mapi.db.TrailerAutocompleteHelpers
.Where(t => t.SearchString.Contains(DatabaseEntities.StripCharacters(userInput, "^A-Z0-9")))
.ToListAsync();
Here's the database function:
CREATE FUNCTION [dbo].[StripCharacters]
(
@String NVARCHAR(MAX),
@MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(MAX) WITH SCHEMABINDING
AS
BEGIN
SET @MatchExpression = '%['+@MatchExpression+']%'
WHILE PatIndex(@MatchExpression, @String) > 0
SET @String = Stuff(@String, PatIndex(@MatchExpression, @String), 1, '')
RETURN @String
END
How can I solve this problem?