2016-11-25 78 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良好的密鑰類型。