How can I make a query using Entity Framework and LINQ on a alphanumeric comparison only? I have a database table with an AccountNumber field which contains special characters, so an example account number might look like this: 803-2234502-345. But the account number that I’m given to search by will not have the special characters: 8032234502345
string accountNumber = "8032234502345";
Provider provider = dbContext.Providers.Where(p => p.AccountNumber == accountNumber).FirstOrDefault();
I’ve tried adding a Regex.Replace to both sides of the comparison, but I get an exception:
LINQ to Entities does not recognize the method ‘System.String Replace(System.String, System.String)’ method, and this method cannot be translated into a store expression.
Any help or suggestions are appreciated.
You are using Linq to Entities so can only use what functions map through to SQL in the enities mappers. You could either change the functions you are using to map properly to SQL (though I suspect with Regex you won’t be-able to do that – would have to be a more manual comparison), or (as @Dabblernl mentioned in the comments) load your Providers (or a subset of providers) into a list / or enumerable, and then run the regex over that.
So something like:
Possible mappings to SQL are here: http://msdn.microsoft.com/en-us/library/bb738681.aspx