I created extension method that Remove Diacritics, in Helper method class,
namespace MvcCms.Models.Utilities
{
public static class HelperMethods
{
public static string RemoveDiacritics(this string stIn)
{
string stFormD = stIn.Normalize(NormalizationForm.FormD);
StringBuilder sb = new StringBuilder();
for (int ich = 0; ich < stFormD.Length; ich++)
{
UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
if (uc != UnicodeCategory.NonSpacingMark)
{
sb.Append(stFormD[ich]);
}
}
return (sb.ToString().Normalize(NormalizationForm.FormC));
}
}
}
and i created a controller that have Json result action searching for the terms without diacritics,
using MvcCms.Models.Utilities;
namespace MvcCms.Controllers
{
public class AjaxController : ControllerHelper
{
public JsonResult AutoCompleteCategories(string term, int id)
{
string termWithoutDiacritics = term.RemoveDiacritics();
var searchResult = db.C_Categories.Where(x => x.CatVocId == id && (x.Title.RemoveDiacritics().ToUpper().Contains(term.ToUpper()) || x.Title.ToUpper().Contains(termWithoutDiacritics.ToUpper())));
var jsonResult = searchResult.Select(results => new { id = results.CatId, name = results.Title });
return Json(jsonResult, JsonRequestBehavior.AllowGet);
}
}
}
when i run the code, it gives me error : LINQ to Entities does not recognize the method 'System.String RemoveDiacritics(System .String)' method, and this method cannot be translated into a store expression