I have the following entity saved at the database:
public class Company
{
public string FullName {get; set;}
}
Property FullName could contains the following data:
"Contoso" Ltd or just Contoso. I am using EF Core and I want to filter the data with this property by short name. I mean if I have "Contoso" Ltd record saved at the database and a user will search for ltd the search result will be null.
For this I tried to use the following LINQ expression:
var result = context.Company.Where(c => c.FullName.ExtractShortNameExtension().Contains(filterString));
with a combination of an extension method:
public static string ExtractShortNameExtension(this string name)
{
int firstQuotes = name.IndexOf('\"');
int lastQuotes = name.LastIndexOf('\"');
if (firstQuotes == -1 || firstQuotes == lastQuotes)
{
return name;
}
return name.Substring(firstQuotes + 1, lastQuotes - firstQuotes - 1);
}
but I get System.InvalidOperationException because LINQ is not able to translate expression. I've tried to use Nein LINQ, but it didn't help me, I have the same exception.
As I realize using of SqlFunctions class might help me to solve the problem, but I want to avoid it, because I want to save the possibility to use my code with the other type of database.
return name => (name.IndexOf('\"') != -1) && (DbFunctions.Reverse(name).IndexOf('\"') != -1) && name.IndexOf('\"') != DbFunctions.Reverse(name).IndexOf('\"') ? name.Substring(name.IndexOf('\"') + 1, DbFunctions.Reverse(name).IndexOf('\"') - name.IndexOf('\"') - 1) : name;but it also throw an exception due to failed translation.