Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从带有非前缀关键字的列表中进行搜索_C#_Search_Autosuggest - Fatal编程技术网

C# 如何从带有非前缀关键字的列表中进行搜索

C# 如何从带有非前缀关键字的列表中进行搜索,c#,search,autosuggest,C#,Search,Autosuggest,我正在编写一个程序,从列表中搜索姓名,我需要找到他们,即使关键字不在姓名前面(这就是我所说的非前缀) 例如如果我的列表是乐器,我在搜索文本框中键入“guit”。 它应该能找到“吉他、吉他手、原声吉他、低音吉他等”的名称 或者像这样的搜索建议 这是我简单而愚蠢的算法(这就是我所能做的) const int SEARCHROWLIMIT=30; 私有字符串[]DoSearch(字符串输入,字符串[]ListToSearch) { List FoundNames=新列表(); int max=0; b

我正在编写一个程序,从列表中搜索姓名,我需要找到他们,即使关键字不在姓名前面(这就是我所说的非前缀)

例如如果我的列表是乐器,我在搜索文本框中键入“guit”。
它应该能找到“吉他、吉他手、原声吉他、低音吉他等”的名称
或者像这样的搜索建议

这是我简单而愚蠢的算法(这就是我所能做的)

const int SEARCHROWLIMIT=30;
私有字符串[]DoSearch(字符串输入,字符串[]ListToSearch)
{
List FoundNames=新列表();
int max=0;
bool over=false;
对于(int k=0;!over;k++)
{
foreach(ListToSearch中的字符串项)
{
max=(max>item.Length)?max:item.Length;
如果(k>项目长度)继续;
如果(k>=max){over=true;break;}
如果(!Input.Equals(“搜索”)
&&item.Substring(k,item.Length-k).StartsWith(Input,StringComparison.OrdinalIgnoreCase))
{
bool exist=false;
int i=0;
而(!exist&&i=SEARCHROWLIMIT)超过=true,则为else;
}
}
}
返回FoundNames.ToArray();
}
我认为这个算法对于大量的名字来说太慢了,经过几次尝试和错误,我决定添加SEARCHROWLIMIT来打破这个操作 我还认为有一些现成的方法可以做到这一点

另一个问题是,我需要按弦乐器、打击乐器等类别搜索乐器。。。以及原籍国。所以我需要按类型和国家用过滤器搜索它们

我怎样才能做到这一点呢?

一个字。数据库

严重的是,如果你想做所有这些不同的搜索,考虑把你的数据放到一个数据库中,这个模式简化了你所拥有的分类问题。Sql Server Express现在支持这一功能,这对于您尝试执行的搜索类型非常有用


有一篇关于将FTS与Linq-to-Sql结合使用的不错的博文。

使用Linq,您可以编写如下代码:

var resultSet = products

    // filter products by category
    .Where(product => product.Category == "strings")

    // filter products by origin
    .Where(product => product.Origin == "italy")

    // filter products whose name contains a word starting with "guit"
    .Where(product => (" " + product.Name).Contains(" guit"))

    // limit the result set to the first 30 matching products
    .Take(30);
如果产品集相当小,则可以使用LINQ对象。否则,您应该使用数据库并查看LINQ to SQL。

静态列表GetItemswithordssStartingwithSubString(列表列表,字符串子字符串)
static List<string> GetItemsWithWordsStartingWithSubstring(List<string> list, string substring)
{
    var query = from str in list
                from item in str.Split(' ')
                where item.StartsWith(substring, StringComparison.InvariantCultureIgnoreCase)
                select str;

    return query.ToList();
}
{ var query=来自列表中的str 从str.Split中的项(“”) where item.StartsWith(子字符串、StringComparison.InvariantCultureInogoreCase) 选择str; 返回query.ToList(); }
我希望我已经正确地阅读了你最初的问题。此函数将返回列表中包含以子字符串开头的单词的任何项目。可以在拆分参数中添加更多标点符号。给出包含以下内容的列表:

“abcdef”、“defabc”、“def abc”、“xyz”


在“abc”上搜索将找到“abcdef”和“def abc”,但不会找到“defabc”。

您的示例仅给出搜索查询位于单词开头的情况。如果它在一个词的中间,比如从<代码> ABCXZDEF 之类的术语中搜索<代码> XYZ < /代码>,请小心。显然,重新提交数据库是不可取的!非常感谢所有的答案。现在我找到了最好的搜索,没有发明任何算法。
static List<string> GetItemsWithWordsStartingWithSubstring(List<string> list, string substring)
{
    var query = from str in list
                from item in str.Split(' ')
                where item.StartsWith(substring, StringComparison.InvariantCultureIgnoreCase)
                select str;

    return query.ToList();
}