.net LINQ to Entities 和 String.StartsWith 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/990451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:51:39  来源:igfitidea点击:

Problem with LINQ to Entities and String.StartsWith

.netlinqentity-frameworklinq-to-entities

提问by ProfK

I'm trying to build a search page using LINQ to Entities, but the following code is giving me a runtime error about l.t.e. not recognising 'Boolean StartsWith(). The code compiles just fine. How can I work around this better than shipping the StartsWith filtering out to a stored proc?

我正在尝试使用 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;

回答by BengtBe

I would guess that EF doesn't support the overload of StartsWith that takes a StringComparison parameter.

我猜想 EF 不支持采用 StringComparison 参数的 StartsWith 重载。

It should support StartsWith, EndsWithand Contains, so maybe you can try:

它应该支持StartsWithEndsWithContains,所以也许你可以尝试:

dp.LastName.StartsWith(searchTerm)

or:

或者:

dp.LastName.ToLower().StartsWith(searchTerm)

and then make sure that searchTermis also lowercase.

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