[英]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.Like
和string.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.