C#如何通过一系列标签在字典中搜索



我正在学习C#,我正在尝试一些简单的东西:输入逗号分隔的一些标签以返回消息。这个想法是,代码应通过我分为数组中的所有标签过滤。

定义变量:

Dictionary<string[], string> messages = new Dictionary<string[], string>();
messages.Add(new string[] { "greeting", "hello", "hei", "hi" }, "Hello!");
messages.Add(new string[] { "greeting", "bye", "buh-bye", "sayonara" }, "Bye!");

获得标签:

string[] tags;
tags.Add("greeting");
tags.Add("hello");

列表和迭代:

List<string> lista = new List<string>();
foreach(string tag in e.GetArg("Tag").Split(','))
{
    foreach (KeyValuePair<string[], string> entry in gifs)
    {
        if (entry.Key.Contains(tag))
        {
            lista.Add(entry.Value);
        }
    }
}

问题是,它在 list 中添加了每个标签的每个MET项目,甚至是再见项目。我可以用一系列标签过滤吗?或者我需要多次通过,每次获得所需的一个?

key数组搜索字典

        Dictionary<string[], string> messages = new Dictionary<string[], string>();
        messages.Add(new string[] { "greeting", "hello", "hei", "hi" }, "Hello!");
        messages.Add(new string[] { "greeting", "bye", "buh-bye", "sayonara" }, "Bye!");
        // add tags
        string[] tags = new string[2];
        tags[0] = "greeting";
        tags[1] = "buh-bye";
        List<string> lista = new List<string>(); // list to store the results
        // loop true the keys from every row
        // loop true the keys of the current row
        // check if tags contains key 
        // ckeck if lista already contains the key that was recognized(no duplicates in list)
        foreach (var value in messages.Keys)
            foreach (var value1 in value)
                if (tags.Contains(value1))
                    if(!lista.Contains(messages[value]))
                        lista.Add(messages[value]);

输出

      ==========
      lista Count = 2
      1: Hello
      2: Bye!

我认为您可以使用简单的类来通过标签搜索。该解决方案将提高代码的抽象水平。课程示例:

public class TagDictionary
{
    Dictionary<string, HashSet<string>> _innerStorage = new Dictionary<string, HashSet<string>>();
    public void Add(IEnumerable<string> tags, string value)
    {
        foreach (var tag in tags)
        {
            if (_innerStorage.ContainsKey(tag))
            {
                var hash = _innerStorage[tag];
                if (!hash.Contains(value))
                {
                    hash.Add(value);
                }
            }
            else
            {
                _innerStorage[tag] = new HashSet<string>
                {
                    value
                };
            }
        }
    }
    public HashSet<string> GetValuesByTags(IEnumerable<string> tags)
    {
        var result = new HashSet<string>();
        foreach (var tag in tags)
        {
            if (_innerStorage.ContainsKey(tag))
            {
                result.UnionWith(_innerStorage[tag]);
            }
        }
        return result;
    }
}

使用示例:

    static void Main(string[] args)
    {
        var messages = new TagDictionary();
        messages.Add(new string[] { "greeting", "hello", "hei", "hi" }, "Hello!");
        messages.Add(new string[] { "greeting", "bye", "buh-bye", "sayonara" }, "Bye!");
        foreach (var value in messages.GetValuesByTags(new[] { "greeting", "hello" }))
        {
            Console.WriteLine(value);
        }
    }

string[]不是标准Dictionary的好键类型。

最新更新