2016-11-25 72 views
3

我正在学习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); 
     } 
    } 
} 

这里的问题是,它增加了每一个遇到的项目为列表中的每个标签,甚至是再见项目。我可以使用一组标签过滤吗?或者我需要通过更多次,每次获得所需的(s)?

回答

3

搜索词典者皆阵列

 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! 
+0

试图执行此操作并对其进行扩展,但出现此错误。 [链接](http://imgur.com/a/EUiK0)。 另外我看到你的变量字典有作为简单的字符串的关键,但我使用项目的字符串[]作为关键,这是否与这样的工作? – yomisimie

+0

o thats因为你有一个字符串[]键作为在词典中的键,我有字符串作为键,只是一秒钟,我会解决它 –

+0

它比我有更好,但问题仍然存在。最后,我需要一个匹配所有标签的数组,现在我有一个匹配任何标签的数组。 – yomisimie

1

我想你可以使用简单的类通过标签搜索。该解决方案将提高代码的抽象级别。 类例如:

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良好的密钥类型。