我的查询返回搜索到的数据,但它没有正确搜索
在我的表中值是
------------------------
Help
------------------------
1 help for abcd
2 help needed before
我在下面给出的Hql查询
select help from Help help where lower(help.Subject) like lower ('%'" + searchterm + "'%')
当我搜索“for”时,它返回
------------------------
Help
------------------------
1 help for abcd
2 help needed before
我只需要返回第一个
1. help for abcd
即:我只需要搜索以搜索词开头的术语
任何人请帮忙......
答案 0 :(得分:1)
答案 1 :(得分:0)
你好,我得到了完美的解决方案
首先使用查询
select help from Help help
然后将帮助列表存储在
中 var ListofHelps
然后
foreach (var item in ListofHelps)
{
if (!string.IsNullOrEmpty(searchterm))
{
var splitsearchterm = Regex.Split(searchterm, @"\s");//split search term
var splittedsubjects = Regex.Split(item.Subject.ToUpper(), @"\s"); //Subject is to be searched
var found = splittedsubjects.Where(x => x.StartsWith(searchterm.ToUpper()));
int datacount = found.Count();
if (splitsearchterm.Count() > 1 && item.Subject.ToUpper().Contains(searchterm.ToUpper()))
{
datacount = 1;
}
if (datacount > 0)
{
Helplist.Add(new HelpViewModel //Helplist is an item in HelpViewModel ie public IEnumerable<MultiSelectList> Taglists { get; set; }
{
Subject = item.Subject,
HelpId = Convert.ToInt32(item.Id),
Content = item.Content
});
}
}
else
{
Helplist.Add(new HelpViewModel
{
Subject = item.Subject,
HelpId = Convert.ToInt32(item.Id),
Content = item.Content
});
}
}
它对我有用。有更好的方法吗