如何使用 EF Core 2.1 从匹配模式的数据库中找到最高值?
How to find highest value from database matching a pattern using EF Core 2.1?
我正在使用 Entity Framework Core 2.1 在 SQL 中存储一个名为 "Equipment" 的 table。 table 有一个名为 "Label" 的列。我有一个名为 Equipment
的 class,标签为字符串 属性。
标签 属性 有时可以有来自用户输入的 #[0-9][0-9][0-9][0-9][0-9]
模式。
如何使用 EF Core 2.1 从 SQL [=37= 快速清洁地从 "Equipment" table 中找到匹配上述模式的最高 "Label" 值]?
var myContext = CreateDbContext();
string resultIdentifier;
// if there is any item in the equipment table
if (myContext.equipment.Any()) {
var regexStr = @"^[#]+(0-9{5})$"; //TODO: how to create this regex string correctly?
// find for any matching pattern in label column using regex
var listFound = myContext.equipment.Where(mp => Regex.IsMatch(mp.Label, regexStr)).ToList();
if (listFound.Any()) {
//TODO: how to find the maximum from the pattern?
}
else {
_logger.Trace("No highest label is found because no matched pattern is found.");
resultIdentifier = null;
}
}
else {
_logger.Trace("No highest label is found because no entry in equipment table.");
resultIdentifier = null;
}
1) 如何创建正确的 Regex 搜索字符串匹配模式 #[0-9][0-9][0-9][0-9][0-9]
?
2) 正则表达式是寻找最高标签模式的最佳方法吗?
3) 如何使用 EF Core 方法从 SQL table 中找到最高标签模式?
谢谢
使用正则表达式效率低下。 EF 无法将 Regex.IsMatch 转换为相应的 SQL 表达式,因此它将首先获取整个设备 table,然后将正则表达式应用于 table 中的每一行。
相反,SQL 服务器具有类似正则表达式的模式和通配符,可以在使用 LIKE 的查询中使用。
考虑以下几点:
DECLARE @foo TABLE (
label varchar(10) null
)
INSERT @foo
SELECT '#12345' -- 'Lowest'
UNION
SELECT '#99999' -- 'Highest'
UNION
SELECT '#999999' -- 'Too Many Characters
UNION
SELECT '#123' -- 'Not Enough Characters'
UNION
SELECT '#abcde1' -- 'Not the Right Characters'
UNION
SELECT '12345' -- 'No Leading #'
SELECT label
FROM @foo
WHERE label LIKE '[#][0-9][0-9][0-9][0-9][0-9]'
将 return 行匹配您的模式(# 字符本身是一个通配符,因此它被括在方括号中以表明它是一个文字):
label
=====
#12345
#99999
SELECT TOP 1 label
FROM @foo
WHERE label LIKE '[#][0-9][0-9][0-9][0-9][0-9]'
ORDER BY label desc
将从高到低排列您的结果:
label
=====
#99999
#12345
并添加 TOP 1 将 return 第一行:
label
=====
#99999
有几种方法可以在 EF Core 中使用它,但最简单的是简单地使用 raw SQL query:
string sql = @"SELECT TOP 1 *
FROM @foo
WHERE label LIKE '[#][0-9][0-9][0-9][0-9][0-9]'
ORDER BY label desc";
var foundRows = myContext.equipment
.FromSql(sql)
.ToList();
if (foundRows.FirstOrDefault() != null)
{
//...do something...
}
我正在使用 Entity Framework Core 2.1 在 SQL 中存储一个名为 "Equipment" 的 table。 table 有一个名为 "Label" 的列。我有一个名为 Equipment
的 class,标签为字符串 属性。
标签 属性 有时可以有来自用户输入的 #[0-9][0-9][0-9][0-9][0-9]
模式。
如何使用 EF Core 2.1 从 SQL [=37= 快速清洁地从 "Equipment" table 中找到匹配上述模式的最高 "Label" 值]?
var myContext = CreateDbContext();
string resultIdentifier;
// if there is any item in the equipment table
if (myContext.equipment.Any()) {
var regexStr = @"^[#]+(0-9{5})$"; //TODO: how to create this regex string correctly?
// find for any matching pattern in label column using regex
var listFound = myContext.equipment.Where(mp => Regex.IsMatch(mp.Label, regexStr)).ToList();
if (listFound.Any()) {
//TODO: how to find the maximum from the pattern?
}
else {
_logger.Trace("No highest label is found because no matched pattern is found.");
resultIdentifier = null;
}
}
else {
_logger.Trace("No highest label is found because no entry in equipment table.");
resultIdentifier = null;
}
1) 如何创建正确的 Regex 搜索字符串匹配模式 #[0-9][0-9][0-9][0-9][0-9]
?
2) 正则表达式是寻找最高标签模式的最佳方法吗?
3) 如何使用 EF Core 方法从 SQL table 中找到最高标签模式?
谢谢
使用正则表达式效率低下。 EF 无法将 Regex.IsMatch 转换为相应的 SQL 表达式,因此它将首先获取整个设备 table,然后将正则表达式应用于 table 中的每一行。
相反,SQL 服务器具有类似正则表达式的模式和通配符,可以在使用 LIKE 的查询中使用。
考虑以下几点:
DECLARE @foo TABLE (
label varchar(10) null
)
INSERT @foo
SELECT '#12345' -- 'Lowest'
UNION
SELECT '#99999' -- 'Highest'
UNION
SELECT '#999999' -- 'Too Many Characters
UNION
SELECT '#123' -- 'Not Enough Characters'
UNION
SELECT '#abcde1' -- 'Not the Right Characters'
UNION
SELECT '12345' -- 'No Leading #'
SELECT label
FROM @foo
WHERE label LIKE '[#][0-9][0-9][0-9][0-9][0-9]'
将 return 行匹配您的模式(# 字符本身是一个通配符,因此它被括在方括号中以表明它是一个文字):
label
=====
#12345
#99999
SELECT TOP 1 label
FROM @foo
WHERE label LIKE '[#][0-9][0-9][0-9][0-9][0-9]'
ORDER BY label desc
将从高到低排列您的结果:
label
=====
#99999
#12345
并添加 TOP 1 将 return 第一行:
label
=====
#99999
有几种方法可以在 EF Core 中使用它,但最简单的是简单地使用 raw SQL query:
string sql = @"SELECT TOP 1 *
FROM @foo
WHERE label LIKE '[#][0-9][0-9][0-9][0-9][0-9]'
ORDER BY label desc";
var foundRows = myContext.equipment
.FromSql(sql)
.ToList();
if (foundRows.FirstOrDefault() != null)
{
//...do something...
}