我必须从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)
任何人都可以提供帮助。
由于
答案 0 :(得分:1)
从你的问题不清楚,但最可能的原因是这一行:
|| p.Name.RemoveDiacritics().Contains(whatToSearch)
我认为此代码正在与数据库联系以根据查询获取产品,并且转换程序无法找到与RemoveDiacritics()
函数类似的SQL。
编辑: - 如果它有效,这将会慢得多,但不过我们可以试试:
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)
)
)
);
}