问题描述
我正在阅读实体框架核心2.0的公告
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/
它说他们添加了新的Sql函数,例如 EF.Functions.Like
来执行SQL Like
操作。
It says that they added new Sql functions like EF.Functions.Like
for performing the SQL LIKE
operation.
我想知道, EF.Functions.Like
和 string。包含
/ StartsWith
?
例如:
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
两个版本之间有什么区别?
EF已经知道如何将 string.Contains
/ StartsWith
转换为相应的SQL操作,但是
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.Functions.Like会允许使用更复杂的模式,例如 a%b%
(尽管可以将其写为 StartsWith( a)&& Contains( b)
)
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")
)
这是原因吗?
推荐答案
像查询一样支持,因此在某些情况下与字符串扩展方法相比非常有用。
Like query supports wildcard characters and hence very useful compared to the string extension methods in some scenarios.
例如:如果要搜索所有以'ri'作为中间字符的4个字母名称,我们可以执行 EF.Functions.Like(c.Name, _ri_) ;
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_");
或从以元音开头的城市中吸引所有客户:
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;
(请阅读@Tseng的答案,了解它们如何不同地转换为SQL查询)
(Please read @Tseng's answer on how they are translated differently into SQL queries)
这篇关于实体框架EF.Functions.Like与字符串。包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!