我必须从 ucommerce 产品中搜索密钥(产品)。
这是代码 -
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();
这是我在循环中使用的扩展方法。
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();
}
我得到了这个例外System.String RemoveDiacritics(System.String)
任何人都可以帮忙。
谢谢