简体   繁体   English

实体框架 EF.Functions.Like 与 string.Contains

[英]Entity framework EF.Functions.Like vs string.Contains

I was reading the announcement of entity framework core 2.0 https://blogs.msdn.microsoft.com/dotnet/2017/08/14/announcing-entity-framework-core-2-0/我正在阅读实体框架核心 2.0 https://blogs.msdn.microsoft.com/dotnet/2017/08/14/annoucing-entity-framework-core-2-0/的公告

It says that they added new Sql functions like EF.Functions.Like for performing the SQL LIKE operation.它说他们添加了新的 Sql 函数,如EF.Functions.Like来执行 SQL LIKE操作。

I was wondering, what then would be the difference between EF.Functions.Like and string.Contains / StartsWith ?我想知道,那么EF.Functions.Likestring.Contains / StartsWith之间有什么区别?

For example:例如:

var customers = context.Customers.Where(c => c.Name.StartsWith("a")); // Version A
var customers = context.Customers.Where(c => EF.Functions.Like(c.Name, "a%")); // Version B

What would be the difference between the two versions?这两个版本之间会有什么区别? EF already knows how to translate string.Contains / StartsWith to the corresponding SQL operations, doesn't it? EF 已经知道如何将string.Contains / StartsWith转换为相应的 SQL 操作,不是吗?

The only reason i can think of is that EF.Functions.Like would allow for more complex patterns like "a%b%" (although this one can be written as StartsWith("a") && Contains("b") )我能想到的唯一原因是 EF.Functions.Like 将允许更复杂的模式,如"a%b%" (虽然这个可以写成StartsWith("a") && Contains("b")

Is this the reason?这是原因吗?

The answer of @adiga is quite incomplete and covers just a part of the differences in usage. @adiga 的答案很不完整,仅涵盖了部分用法差异。

However, .StartsWith(...) , .Contains(...) and .EndsWith(...) are also translated differently into SQL then EF.Functions.Like .但是, .StartsWith(...).Contains(...).EndsWith(...)也以不同的方式转换为 SQL 然后EF.Functions.Like

For example .StartsWith gets translated as (string LIKE pattern + "%" AND CHARINDEX(pattern, string) = 1) OR pattern = '' where .Contains gets translated into (CHARINDEX(pattern, string) > 0) OR pattern = '' .例如.StartsWith被翻译为(string LIKE pattern + "%" AND CHARINDEX(pattern, string) = 1) OR pattern = '' where .Contains被翻译成(CHARINDEX(pattern, string) > 0) OR pattern = ''

EF.Functions.Like however gets translated into string LIKE pattern [ESCAPE escapeChar] . EF.Functions.Like然而被翻译成string LIKE pattern [ESCAPE escapeChar]

This may also have implications on Performance.这也可能对性能产生影响。 The above is valid for EF Core SqlServer provider.以上适用于 EF Core SqlServer提供程序。 Other EF Core providers may translate it differently.其他 EF Core 提供商可能会以不同的方式对其进行翻译。

Like query supports wildcard characters and hence very useful compared to the string extension methods in some scenarios. Like 查询支持通配符,因此在某些情况下与字符串扩展方法相比非常有用。

For ex: If we were to search all the 4 lettered names with 'ri' as the middle characters we could do EF.Functions.Like(c.Name, "_ri_");例如:如果我们要搜索所有以 'ri' 作为中间字符的 4 个字母名称,我们可以执行EF.Functions.Like(c.Name, "_ri_");

or to get all the customers from cities which start with vowels:或者从以元音开头的城市中获取所有客户:

var customers = from c in context.Customers 
                   where EF.Functions.Like(c.City, "[aeiou]%")
                   select c;

(Please read @Tseng's answer on how they are translated differently into SQL queries) (请阅读@Tseng 的回答,了解它们如何以不同的方式转换为 SQL 查询)

@adiga is quite incomplete and covers just a part of the differences in usage. @adiga非常不完整,仅涵盖部分用法差异。

However, .StartsWith(...) , .Contains(...) and .EndsWith(...) are also translated differently into SQL then EF.Functions.Like . 然而, .StartsWith(...) .Contains(...).EndsWith(...)也被翻译成不同的SQL然后EF.Functions.Like

For example .StartsWith gets translated as (string LIKE pattern + "%" AND CHARINDEX(pattern, string) = 1) OR pattern = '' where .Contains gets translated into (CHARINDEX(pattern, string) > 0) OR pattern = '' . 例如, .StartsWith被转换为(string LIKE pattern + "%" AND CHARINDEX(pattern, string) = 1) OR pattern = ''其中.Contains被转换为(CHARINDEX(pattern, string) > 0) OR pattern = ''

EF.Functions.Like however gets translated into string LIKE pattern [ESCAPE escapeChar] . 但是, EF.Functions.Like被转换为string LIKE pattern [ESCAPE escapeChar]

This may also have implications on Performance. 这可能也会影响性能。 The above is valid for EF Core SqlServer provider. 上面的内容对EF Core SqlServer提供程序有效。 Other EF Core providers may translate it differently. 其他EF Core提供者可能会进行不同的翻译。

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

相关问题 EF Core 5 - 如何将 EF.Functions.Like 与映射到 JSON 字符串的自定义属性一起使用? - EF Core 5 - How can I use EF.Functions.Like with a custom property that maps to a JSON string? EF Core - Select 所有列使用 EF.Functions.Like - EF Core - Select all columns using EF.Functions.Like 强制不区分大小写的String.Contains in Entity Framework Core - Forcing Case Insensitive String.Contains in Entity Framework Core 正则表达式vs字符串包含 - Regex vs String.Contains 如何使 EF.Functions.Like 不区分大小写? - How to make EF.Functions.Like case-insensitive? EF.Functions.Like 在已编译查询中无法转换为 SQL 等效项 - EF.Functions.Like in a compiled query could not be translated to SQL equivalent 升级到 .NET 6 后,EF.Functions.Like 方法不起作用 - EF.Functions.Like method not working after upgrading to .NET 6 使用 String.Contains 的 EF Core 查询 - EF Core query using String.Contains EF Core - 我们可以使用 EF.Functions.Like 执行动态数量的 OR 运算符吗? - EF Core - Can we do a dynamic number of OR operators with EF.Functions.Like? Linq WHERE EF.Functions.Like - 为什么直接属性有效而反射无效? - Linq WHERE EF.Functions.Like - Why direct properties work and reflection does not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM