3

.NET Core 2.2.0

I would like to use Wildcards in EF Core's Like function, but it doesn't work the way I expect and I've read about in some posts (best example here)

My Code:

List<string> list = new List<string>();
list.Add("Hi fransois");
list.Add("Hi francois");
list.Add("Hi françois");

List<string> testa = list.Where(a => EF.Functions.Like(a, "%francois%")).ToList();      // Results in 1 hit, as expected
List<string> testb = list.Where(b => EF.Functions.Like(b, "%françois%")).ToList();      // Results in 1 hit, as expected
List<string> testc = list.Where(c => EF.Functions.Like(c, "%fran[cç]ois%")).ToList();   // Results in 0 hits, EXPECTED: 2

Why doen't this work as expected?

CribAd
  • 452
  • 6
  • 19
  • Provide sample data please. – Ryan Wilson Apr 15 '19 at 14:13
  • 2
    No, those calls won't return anything, because they're [only supported in LINQ to Entities queries](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.ef.functions?view=efcore-2.1). You're using LINQ to Objects. Read [ask] and create a [mcve], including your particular database provider. – CodeCaster Apr 15 '19 at 14:13
  • Actually this is my whole code for testing, placed in my OnGet Method (ASP.Net Core Razor Page). testa contains "Hi francois" and testb contains "Hi françois" so the like-methods work on my list. But when using a wildcard, it returns nothing. – CribAd Apr 15 '19 at 14:18
  • @stuartd tested, doesn't work. – CribAd Apr 15 '19 at 14:27
  • Then the docs are probably outdated again, nevermind my comment then. – CodeCaster Apr 15 '19 at 14:36
  • 1
    @stuartd the commas are separators of accepted wildcard characters, i.e. `%`, `_`, `[`, `]`, `^`. The accepted format depends on the database provider. SQL Server should accept `[cç]`. – CodeCaster Apr 15 '19 at 14:37
  • @CodeCaster ahh ok thanks – stuartd Apr 15 '19 at 14:44
  • Check this out [https://stackoverflow.com/questions/1040380/wildcard-search-for-linq](https://stackoverflow.com/questions/1040380/wildcard-search-for-linq) – ammad khan Apr 17 '19 at 06:37
  • @ammadkhan Yes I saw that post, and as you can see in my answer below; Regex works. But I still don't understand why it will not work with `EF.Functions.Like(c, "%fran[cç]ois%")`. – CribAd Apr 17 '19 at 12:24

1 Answers1

0

Unfortunately it looks like this isn't gonna work. But there is a workaround using Regex:

    Regex regex = new Regex("fran[cç]ois");
    List<string> testd = list.Where(d => regex.IsMatch(d)).ToList();

This works.

CribAd
  • 452
  • 6
  • 19