LINQ to SQL 查找句子中单词以特定字符开头的记录
LINQ to SQL find records where words in a sentence start with specific characters
我有一个 SQL 服务器 table:
ID | Code | Name
1 | AS | Andrew Smith
2 | RA | Ryan Andrews
3 | ZS | Zach Simmons
我想查找名称列中代码开头或名字或姓氏开头与关键词匹配的记录。例如,如果关键词是 'A',它应该 return:
1 | AS | Andrew Smith
2 | RA | Ryan Andrews
因为 Andre Smith 的代号和名字与 'A' 匹配,而 Andre Ryan 的姓氏与 'A' 匹配。
我目前的查询是:
var keyphrase = 'aa';
var employees = await (from e in _dbContext.Employee
where EF.Functions.Like(e.Code, $"{keyphrase}%")
orderby e.Code ascending
select e).ToListAsync();
这对于根据代码进行搜索非常有效。但我不确定如何将关键词与名称列中的名字和姓氏相匹配。同样,我不想做一个 Contains,而是一个 StartsWith。
我正在使用 EF 核心 3.1。
感谢任何帮助。
这里是一个Nuget包方式,如果你想要classes:
,你可以从Git Hub获取源代码
Nuget:DataJuggler.UltimateHelper.Core
using DataJuggler.Core.UltimateHelper;
using DataJuggler.Core.UltimateHelper.Objects;
// set the text
string text = "For a good night's sleep before bed, avoid caffiene.";
// split into lines if needed
List<TextLine> lines = WordParser.GetTextLines(text);
// verify the lines exist and have one or more items
if (ListHelper.HasOneOrMoreItems(lines))
{
// Iterate the collection of TextLine objects
foreach (TextLine textLine in lines)
{
// get the words
List<Word> words = WordParser.GetWords(textLine.Text);
// If the words collection exists and has one or more items
if (ListHelper.HasOneOrMoreItems(words))
{
// Iterate the collection of Word objects
foreach (Word word in words)
{
if (word.Text.ToLower().StartsWith("a"))
{
// do something for a
}
else if (word.Text.ToLower().StartsWith("b"))
{
// do something else for b
}
else if (word.Text.ToLower().StartsWith("c"))
{
// do something else for c
}
}
}
}
}
如果你没有多行,你可以跳过解析行部分,但我已经使用这个 class 多年并且喜欢它。
完整的源代码在 Nuget 上:
https://github.com/DataJuggler/DataJuggler.UltimateHelper.Core
StartsWith
应与 EF Core 一起使用 - 它被转换为 SQL LIKE
并带有尾随 %
。我建议使用它 Contains
:
var keyphrase = 'aa';
var employees = await (from e in _dbContext.Employee
where e.Code.StartsWith(keyphrase) || e.Name.StartsWith(keyphrase) || e.Name.Contains($" {keyphrase}")
orderby e.Code ascending
select e).ToListAsync();
假设您在要检查的每个术语之前使用 space (" "
),那么您可以使用以下技巧:
where EF.Functions.Like(" " + e.Name, "% " + keyphrase + "%")
请注意列值和关键字前面的 space。列前的 space 用于处理列值开头的关键短语。
或者,您可以使用 2 个单独的条件来匹配开始项和中间项:
where EF.Functions.Like(e.Name, keyphrase + "%")
|| EF.Functions.Like(e.Name, "% " + keyphrase + "%")
我有一个 SQL 服务器 table:
ID | Code | Name
1 | AS | Andrew Smith
2 | RA | Ryan Andrews
3 | ZS | Zach Simmons
我想查找名称列中代码开头或名字或姓氏开头与关键词匹配的记录。例如,如果关键词是 'A',它应该 return:
1 | AS | Andrew Smith
2 | RA | Ryan Andrews
因为 Andre Smith 的代号和名字与 'A' 匹配,而 Andre Ryan 的姓氏与 'A' 匹配。
我目前的查询是:
var keyphrase = 'aa';
var employees = await (from e in _dbContext.Employee
where EF.Functions.Like(e.Code, $"{keyphrase}%")
orderby e.Code ascending
select e).ToListAsync();
这对于根据代码进行搜索非常有效。但我不确定如何将关键词与名称列中的名字和姓氏相匹配。同样,我不想做一个 Contains,而是一个 StartsWith。
我正在使用 EF 核心 3.1。
感谢任何帮助。
这里是一个Nuget包方式,如果你想要classes:
,你可以从Git Hub获取源代码Nuget:DataJuggler.UltimateHelper.Core
using DataJuggler.Core.UltimateHelper;
using DataJuggler.Core.UltimateHelper.Objects;
// set the text
string text = "For a good night's sleep before bed, avoid caffiene.";
// split into lines if needed
List<TextLine> lines = WordParser.GetTextLines(text);
// verify the lines exist and have one or more items
if (ListHelper.HasOneOrMoreItems(lines))
{
// Iterate the collection of TextLine objects
foreach (TextLine textLine in lines)
{
// get the words
List<Word> words = WordParser.GetWords(textLine.Text);
// If the words collection exists and has one or more items
if (ListHelper.HasOneOrMoreItems(words))
{
// Iterate the collection of Word objects
foreach (Word word in words)
{
if (word.Text.ToLower().StartsWith("a"))
{
// do something for a
}
else if (word.Text.ToLower().StartsWith("b"))
{
// do something else for b
}
else if (word.Text.ToLower().StartsWith("c"))
{
// do something else for c
}
}
}
}
}
如果你没有多行,你可以跳过解析行部分,但我已经使用这个 class 多年并且喜欢它。
完整的源代码在 Nuget 上: https://github.com/DataJuggler/DataJuggler.UltimateHelper.Core
StartsWith
应与 EF Core 一起使用 - 它被转换为 SQL LIKE
并带有尾随 %
。我建议使用它 Contains
:
var keyphrase = 'aa';
var employees = await (from e in _dbContext.Employee
where e.Code.StartsWith(keyphrase) || e.Name.StartsWith(keyphrase) || e.Name.Contains($" {keyphrase}")
orderby e.Code ascending
select e).ToListAsync();
假设您在要检查的每个术语之前使用 space (" "
),那么您可以使用以下技巧:
where EF.Functions.Like(" " + e.Name, "% " + keyphrase + "%")
请注意列值和关键字前面的 space。列前的 space 用于处理列值开头的关键短语。
或者,您可以使用 2 个单独的条件来匹配开始项和中间项:
where EF.Functions.Like(e.Name, keyphrase + "%") || EF.Functions.Like(e.Name, "% " + keyphrase + "%")