0

我必须从 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)

任何人都可以帮忙。

谢谢

4

1 回答 1

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)
                )
            )
    );
}
于 2017-03-14T01:21:44.163 回答