如何从具有非前缀关键字的列表中进行搜索 [英] How to do a search from a list with non-prefix keywords

查看:54
本文介绍了如何从具有非前缀关键字的列表中进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来从列表中搜索名称,即使关键字不在名称前面,我也需要找到它们(这就是我的意思是非前缀)

I am programming a program to search the name from the list and I need to find them even if the keyword is not in front of the names (that's what I mean non-prefix)

例如如果我的列表是乐器并且我在搜索文本框中输入guit".
它应该找到名称​​Guitar、Guitarrón、Acoustic Guitar、Bass Guitar,..."
或类似Longdo Dictionary的搜索建议.

e.g. if I my list is the music instruments and I type "guit" to the search textbox.
It should find the names "Guitar, Guitarrón, Acoustic Guitar, Bass Guitar, ..."
or something like this Longdo Dictionary's search suggestion.

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

here is my simple and stupid algorithm (that's all I can do)

    const int SEARCHROWLIMIT = 30;
    private string[] DoSearch(string Input, string[] ListToSearch)
    {
        List<string> FoundNames = new List<string>();

        int max = 0;
        bool over = false;
        for (int k = 0; !over; k++)
        {
            foreach (string item in ListToSearch)
            {
                max = (max > item.Length) ? max : item.Length;
                if (k > item.Length) continue;
                if (k >= max) { over = true; break; }
                if (!Input.Equals("Search")
                    && item.Substring(k, item.Length - k).StartsWith(Input, StringComparison.OrdinalIgnoreCase))
                {
                    bool exist = false;
                    int i = 0;
                    while (!exist && i < FoundNames.Count)
                    {
                        if (item.Equals(FoundNames[i]))
                        {
                            exist = true;
                            break;
                        }
                        i++;
                    }
                    if (!exist && FoundNames.Count < SEARCHROWLIMIT)
                        FoundNames.Add(item);
                    else if (FoundNames.Count >= SEARCHROWLIMIT) over = true;
                }
            }
        }
        return FoundNames.ToArray();
    }

我认为这个算法对于大量名字来说太慢了,经过几次反复试验,我决定添加 SEARCHROWLIMIT 来中断操作而且我还认为有一些现成的方法可以做到这一点.

I think this algorithm is too slow for a large number of names and after several trial-and-error, I decided to add SEARCHROWLIMIT to breaks the operation And I also think there're some readymade methods that can do that.

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

And another problem is I need to search music instruments by a category like strings, percussions, ... and by the country of origins. So I need to search them with filter by type and country.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

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

Using LINQ you could write code like this:

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-to-Objects.否则,您应该使用数据库并查看 LINQ-to-SQL.

If your sets of products is reasonably small, you can use LINQ-to-Objects. Otherwise you should use a database and have a look at LINQ-to-SQL.

这篇关于如何从具有非前缀关键字的列表中进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆