LINQ to Entities和String.StartsWith的问题

Pro*_*ofK 49 .net linq linq-to-entities entity-framework

我正在尝试使用LINQ to Entities构建搜索页面,但是下面的代码给出了一个运行时错误,关于lte没有识别'Boolean StartsWith().代码编译得很好.我怎样才能解决这个问题,而不是将StartsWith过滤到存储过程?

    return from dp in dents.DirectoryPersonEntrySet
           where
               ((dp.LastName.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase)) ||
                (dp.Department.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase)) ||
                dp.Extension.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase))
           select dp;
Run Code Online (Sandbox Code Playgroud)

Ben*_*tBe 92

我猜想EF不支持带有StringComparison参数的StartsWith的重载.

它应该支持StartsWith,EndsWithContains,所以你可以尝试:

dp.LastName.StartsWith(searchTerm)
Run Code Online (Sandbox Code Playgroud)

要么:

dp.LastName.ToLower().StartsWith(searchTerm)
Run Code Online (Sandbox Code Playgroud)

然后确保它searchTerm也是小写的.

  • 可能(可能)不区分大小写.这取决于数据库排序规则设置 (5认同)
  • 死了,整个字符串选项thang抛出EF (2认同)
  • 猜对了,但请注意,它在转换为SQL时不区分大小写. (2认同)
  • 它将其转换为分别包含 'my string%'、'%my string' 和 '%my string%' 的 LIKE 语句,该语句自动不区分大小写,并且无法通过此调用将其指定为区分大小写 (2认同)