0

I have a Worker entity with string Name property, and just want to filter all Workers whose Name contains some specific string (from frontend text input).

When I do the filtering with:

_context.Workers.Where(w => w.Name.ToUpper().Contains(filter.ToUpper()).ToList()

it is working but it does not solves some specific diacritics in filter term.

When i try with:

var compareInfo = CultureInfo.InvariantCulture.CompareInfo;
 _context.Workers.Where(w => compareInfo.IndexOf(w.Name, filter, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) > -1).ToList()

i get System.NullReferenceException: 'Object reference not set to an instance of an object.' and console An exception occurred in the database while iterating the results of a query for context type 'Project.MyDbContext'.

I've also tried with

_context.Workers.Where(w => compareInfo.IndexOf((w.Name??""), filter, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) > -1).ToList()

to perform a null check, but same.

Did anyone have same problem, or maybe idea what could be changed here so I could accompiish searching with diacritics?

Thx!

zlaayaa
  • 585
  • 2
  • 6
  • 25
  • You should tackle this kind of issues by using proper database collation. After all, the query is executed as SQL in the database. More precisely: the actual query *should* be executed in the database. Whatever you add to it that's not supported in C# to SQL conversion will cause EF-core to switch to client-side evaluation and all data in `_context.Workers` will be pulled into memory. – Gert Arnold Jul 13 '18 at 07:16
  • Ok, any proposal how to do that from C#? – zlaayaa Jul 13 '18 at 07:25
  • Probably *not* by using EF: https://stackoverflow.com/q/11005036/861716 – Gert Arnold Jul 13 '18 at 07:30

0 Answers0