I have to search for a key (product) from ucommerce products.
Here is the code -
string whatToSearch = "L'oreal";
var normalizedText = whatToSearch.Normalize(NormalizationForm.FormD);
whatToSearch = nonSpacingMarkRegex.Replace(normalizedText, string.Empty);
whatToSearch = Regex.Replace(whatToSearch, @"[^0-9a-zA-Z ]+", "");
var products = new List<UCommerce.EntitiesV2.Product>();
if (!string.IsNullOrWhiteSpace(whatToSearch))
{
products = UCommerce.EntitiesV2.Product.Find(p =>
p.VariantSku == null && p.DisplayOnSite &&
(
p.Sku.Contains(whatToSearch)
|| p.Name.RemoveDiacritics().Contains(whatToSearch)
|| p.ProductDescriptions.Any(
d => d.DisplayName.Contains(whatToSearch)
|| d.ShortDescription.Contains(whatToSearch)
|| d.LongDescription.Contains(whatToSearch)
)
)
);
}
var productIds = products.Select(x => x.Id).ToList();
And this is the extension method I am using inside the loop.
public static string RemoveDiacritics(this string s)
{
var normalizedString = s.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();
foreach (var c in normalizedString)
{
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
stringBuilder.Append(c);
}
return stringBuilder.ToString();
}
I am getting this exception System.String RemoveDiacritics(System.String)
Can anyone help.
Thanks
Not clear from your question, but most probable cause is this line:
|| p.Name.RemoveDiacritics().Contains(whatToSearch)
I think this code is contacting to the database to fetch products based on the query and translator cannot find SQL analogue to RemoveDiacritics()
functions.
EDIT:- Sure this will be a lot slower if it works, but lets try anyway:
if (!string.IsNullOrWhiteSpace(whatToSearch))
{
//I've added call to ALL to fetch all products and then filter on
//code side.
products = UCommerce.EntitiesV2.Product.All().Where(p =>
p.VariantSku == null && p.DisplayOnSite &&
(
p.Sku.Contains(whatToSearch)
|| p.Name.RemoveDiacritics().Contains(whatToSearch)
|| p.ProductDescriptions.Any(
d => d.DisplayName.Contains(whatToSearch)
|| d.ShortDescription.Contains(whatToSearch)
|| d.LongDescription.Contains(whatToSearch)
)
)
);
}