如何在EF Core Like函数中使用通配符

时间:2019-04-15 14:08:37

标签: c# entity-framework .net-core entity-framework-core

.NET Core 2.2.0

我想在EF Core的Like函数中使用通配符,但是它不能按我期望的方式工作,我已经在一些帖子中读到过(最佳示例here

我的代码:

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

为什么这不能正常工作?

1 个答案:

答案 0 :(得分:0)

不幸的是,这看起来行不通。但是有一种使用Regex的解决方法:

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

这有效。