0

I created a search method and if I'm looking for a word (for ex: "ünsüz") , I want to search this word when I type "unsuz" too. For this I'm trying to convert "ünsüz" to "unsuz" but I get an error. What am I doing wrong here?

 public PartialViewResult Search(string searchKey)
        {
            string infoItem = "";
            var shortDescItem = "";
            viewModel = new SearchModel();
            viewModel.SearchKey = searchKey;

            var unaccentedText = String.Join("", searchKey.Normalize(NormalizationForm.FormD)
                    .Where(c => char.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)).Replace('ş', 's').Replace('ç', 'c').Replace('ı', 'i').Replace('ü', 'u').Replace('ö', 'o').Replace('ğ', 'g');

            foreach (var item in _context.Test.Where(m => m.IsDeleted == false && (m.StartDate < DateTime.Now && m.EndDate > DateTime.Now || m.StartDate == DateTime.MinValue || m.EndDate == DateTime.MinValue)).OrderBy(m => m.Order).ToList())
            {
                infoItem = item.Info.ToLower().Replace('ş', 's').Replace('ç', 'c').Replace('ı', 'i').Replace('ü', 'u').Replace('ö', 'o').Replace('ğ', 'g');
                shortDescItem = item.ShortDescription.ToLower().Replace('ş', 's').Replace('ç', 'c').Replace('ı', 'i').Replace('ü', 'u').Replace('ö', 'o').Replace('ğ', 'g');
            }
            viewModel.TestList = _context.Test.Where(infoItem.Contains(unaccentedText).ToString()).ToList();
           

            return PartialView("ResultView", viewModel);

        }

Where(infoItem.Contains(unaccentedText) shows up an error as cannot convert from 'string' to 'System.Linq.Expressions.Expression<System.Func<Project.Application.Entities.TestEntity, bool>>'

  • maybe something like this https://stackoverflow.com/questions/7470997/replace-german-characters-umlauts-accents-with-english-equivalents the second answer seems pretty okay. though neither look to be completely optimal. It might be able to optimize it through a stream reader and stream writer to read the string character by character while converting it into a new string, without so much memory duplication. – Joe_DM Nov 08 '22 at 06:56
  • or maybe look for an existing package. like this https://www.nuget.org/packages/Diacritics but I'm sure theres others – Joe_DM Nov 08 '22 at 06:58
  • Possible duplication by (https://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net) – Ali Nov 08 '22 at 07:21

0 Answers0