简体   繁体   English

EF Like 操作符用法

[英]EF Like operator usage

I tried to use DbFunctions.Like with EF 6.2 and got run-time error:我尝试将DbFunctions.Like与 EF 6.2 一起使用,但出现运行时错误:

LINQ to Entities does not recognize the method 'Boolean Like(System.String, System.String)' method, and this method cannot be translated into a store expression. LINQ to Entities 无法识别方法 'Boolean Like(System.String, System.String)' 方法,并且此方法无法转换为存储表达式。

Code:代码:

list=list.Where(p=> DbFunctions.Like(p.Master_Bill,"somestring%")); where list is IQueryable<SomeView>

It compiles OK.它编译正常。 I thought it can be used with EF 6.2.我认为它可以与 EF 6.2 一起使用。 I know there is also EF Core, did not look at it我知道也有EF Core,没看

Any ideas?有任何想法吗? thanks谢谢

What about关于什么

list = list.Where( p => p.Master_Bill.StartsWith(someString));

? ?

If the user can enter a wildcard(s) in their search string you can evaluate the string for legal combinations Ie "?somestring", "somestring?"如果用户可以在他们的搜索字符串中输入通配符,您可以评估字符串的合法组合,即“?somestring”、“somestring?” or "?somestring?"或“?一些字符串?” then choose the appropriate where condition.然后选择合适的 where 条件。

wildcardResult = evaluateWildcard(someString);
switch(wildcardResult.Result)
{
    case WildcardResult.NoWildcard:
        list = list.Where( p => p.Master_Bill == wildcardResult.SearchString);
        break;
    case WildcardResult.StartsWith:
        list = list.Where( p => p.Master_Bill.StartsWith(wildcardResult.SearchString));
        break;
    case WildcardResult.EndsWith:
        list = list.Where( p => p.Master_Bill.EndsWith(wildcardResult.SearchString));
        break;
    case WildcardResult.Contains:
        list = list.Where( p => p.Master_Bill.Contains(wildcardResult.SearchString));
        break;
}

Where the result class contains an enum for the detected search expression pattern, and the search expression with the wildcard characters stripped to use as the SearchString.其中结果类包含检测到的搜索表达式模式的enum ,以及去除通配符以用作 SearchString 的搜索表达式。

It would also be advisable to evaluate the length of the search string for a minimum viable length when using wildcards.还建议在使用通配符时评估搜索字符串的长度以获得最小可行长度。 Users could trigger rather expensive queries by using expressions like "?"用户可以通过使用诸如“?”之类的表达式来触发相当昂贵的查询。 or "?e?".或“?e?”。

Edit: DbFunctions.Like does work as well with SQL Server.编辑: DbFunctions.Like也适用于 SQL Server。 Any error you are getting is likely due to an assumption about the IQueryable you are running or the field you are comparing.您遇到的任何错误都可能是由于对您正在运行的IQueryable或您正在比较的字段的假设。 (Ie not a mapped column, or a particular data type?) (即不是映射列或特定数据类型?)

For instance: Something like this works just fine..例如:这样的东西工作得很好..

var data = _context.People.Where(p => DbFunctions.Like(p.Name, "s%")).ToList();

Which would return all People with a Name starting with "S".这将返回名称以“S”开头的所有人员。 (case insensitive) (不区分大小写)

I'd look at what your entire IQueryable looks like, as well as that Master_Bill is both a mapped column and a regular NVARCHAR/VARCHAR column.我会看看你的整个IQueryable是什么样子,以及 Master_Bill 既是映射列又是常规 NVARCHAR/VARCHAR 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM