I'm searching on a SQL 2005 DB that has a fullname column in it. I want to be able to check against that column and order the results according to their relevance.
我正在搜索其中包含fullname列的SQL 2005 DB。我希望能够检查该列并根据其相关性对结果进行排序。
I'm using a nice ordering system that I found on SO but that unfortunately is not working as I'd like, and it seems to be because of the 'contains' functionality not matching.
我正在使用一个很好的订购系统,我在SO上找到了,但不幸的是,它并没有像我想的那样工作,而且似乎是因为“包含”功能不匹配。
Here's the code:
这是代码:
private static int QueryOrder(string query, string name)
{
if (name == query)
return -1;
if (name.Contains(query))
return 0;
return 1;
}
Now the problem is that this doesn't work in the following scenario:
现在的问题是,这在以下场景中不起作用:
Database fullname column data: Joey JoeJoe Junior Shabbadoo
数据库全名列数据:Joey JoeJoe Junior Shabbadoo
Search query: Joey Shabbadoo
搜索查询:Joey Shabbadoo
The queryorder method doesn't match on the 'contains' as it doesn't see the fullname field as containing the query text.
queryorder方法与'contains'不匹配,因为它没有将fullname字段视为包含查询文本。
The way I'm then ordering the results is like this
我之后订购结果的方式是这样的
peopleList = peopleList.OrderBy(p => QueryOrder(fullName, p.FullName)).ToList();
So, is there a way using LINQ to do this or should I just go down the sproc route?
那么,有没有办法使用LINQ来做这个或者我应该沿着sproc路线走?
1 个解决方案
#1
LINQ is right. "Joey Shabbado" is not contained
in your search string. For contains to return true, complete sequence of characters must match one complete sequence in the other string.
LINQ是对的。 “Joey Shabbado”未包含在您的搜索字符串中。要使contains返回true,则完整的字符序列必须与另一个字符串中的一个完整序列匹配。
Depending on what you define as 'matches' you could turn to regular expressions and try (1) splitting your query and then matching against \AJoey.+Shabbadoo\z
using regex
根据您定义为“匹配”的内容,您可以转向正则表达式并尝试(1)拆分查询,然后使用正则表达式匹配\ AJoey。+ Shabbadoo \ z
#1
LINQ is right. "Joey Shabbado" is not contained
in your search string. For contains to return true, complete sequence of characters must match one complete sequence in the other string.
LINQ是对的。 “Joey Shabbado”未包含在您的搜索字符串中。要使contains返回true,则完整的字符序列必须与另一个字符串中的一个完整序列匹配。
Depending on what you define as 'matches' you could turn to regular expressions and try (1) splitting your query and then matching against \AJoey.+Shabbadoo\z
using regex
根据您定义为“匹配”的内容,您可以转向正则表达式并尝试(1)拆分查询,然后使用正则表达式匹配\ AJoey。+ Shabbadoo \ z