使用 Entity Framework 搜索删除逗号
Search removing comma using Entity Framework
我想在数据库中搜索包含逗号的文本,但参考文献中没有逗号。
例如。在数据库中,我有以下值:
"Development of computer programs, including electronic games"
因此,我尝试使用以下字符串作为参考来搜索数据:
"development of computer programs including electronic games"
注意唯一的区别是在数据库中我在文本中有一个逗号,但是在我的搜索参考中,我没有。
这是我的代码:
public async Task<ActionResult>Index(string nomeServico)
{
using (MyDB db = new MyDB())
{
// 1st We receive the following string:"development-of-computer-programs-including-electronic-games"
// but we remove all "-" characters
string serNome = nomeServico.RemoveCaractere("-", " ");
// we search the service that contains (in the SerName field) the value equal to the parameter of the Action.
Servicos servico = db.Servicos.FirstOrDefault(c => c.SerNome.ToLower().Equals(serNome, StringComparison.OrdinalIgnoreCase));
}
}
问题是,在数据库中,数据包含逗号,而在搜索值中,不包含逗号。
在您的代码中,您将“-”替换为“”,并且在您的搜索字符串中也是如此。但是根据您的要求,您需要将数据库条目的“,”更改为“”。
尝试做这样的事情:
string serNome = nomeServico.ToLower();
Servicos servico = db.Servicos.FirstOrDefault(c => c.SerNome.Replace(",","").ToLower() == serNome);
希望对您有所帮助!!
我想在数据库中搜索包含逗号的文本,但参考文献中没有逗号。
例如。在数据库中,我有以下值: "Development of computer programs, including electronic games"
因此,我尝试使用以下字符串作为参考来搜索数据: "development of computer programs including electronic games"
注意唯一的区别是在数据库中我在文本中有一个逗号,但是在我的搜索参考中,我没有。
这是我的代码:
public async Task<ActionResult>Index(string nomeServico)
{
using (MyDB db = new MyDB())
{
// 1st We receive the following string:"development-of-computer-programs-including-electronic-games"
// but we remove all "-" characters
string serNome = nomeServico.RemoveCaractere("-", " ");
// we search the service that contains (in the SerName field) the value equal to the parameter of the Action.
Servicos servico = db.Servicos.FirstOrDefault(c => c.SerNome.ToLower().Equals(serNome, StringComparison.OrdinalIgnoreCase));
}
}
问题是,在数据库中,数据包含逗号,而在搜索值中,不包含逗号。
在您的代码中,您将“-”替换为“”,并且在您的搜索字符串中也是如此。但是根据您的要求,您需要将数据库条目的“,”更改为“”。
尝试做这样的事情:
string serNome = nomeServico.ToLower();
Servicos servico = db.Servicos.FirstOrDefault(c => c.SerNome.Replace(",","").ToLower() == serNome);
希望对您有所帮助!!