【问题标题】:How to get the value of key in List which has Hashtable如何获取具有Hashtable的List中键的值
【发布时间】:2016-04-18 10:17:01
【问题描述】:
public static List<Hashtable> StoreSearchResult(NgWebDriver driver)
{
    List<Hashtable> searchList = new List<Hashtable>();
    Hashtable table = new Hashtable();
   // List<SEarchResult> searchResultList = new List<SEarchResult>();
    int numberofrows = driver.FindElements(By.XPath("//*[@id='contenttablegridsearchModal']/div")).Count;

    for (int i = 0; i < numberofrows; i++)
    {
        table.Add("ID",driver.FindElement(By.XPath("//*[@id='row" + i + "gridsearchModal']/div[2]/div")).Text);
        table.Add("Date", driver.FindElement(By.XPath("//*[@id='row" + i + "gridsearchModal']/div[3]/div")).Text);
        table.Add("Type", driver.FindElement(By.XPath("//*[@id='row" + i + "gridsearchModal']/div[4]/div")).Text);
        table.Add("Sub-Type", driver.FindElement(By.XPath("//*[@id='row" + i + "gridsearchModal']/div[5]/div")).Text);
        table.Add("Description", driver.FindElement(By.XPath("//*[@id='row" + i + "gridsearchModal']/div[6]/div")).Text);
        table.Add("Score", driver.FindElement(By.XPath("//*[@id='row" + i + "gridsearchModal']/div[7]/div")).Text);
        table.Add("Asssigned-To", driver.FindElement(By.XPath("//*[@id='row" + i + "gridsearchModal']/div[8]/div")).Text);
        table.Add("Close-Reason", driver.FindElement(By.XPath("//*[@id='row" + i + "gridsearchModal']/div[9]/div")).Text);
    }
    searchList.Add(table);
    int sizeoftable = table.Count;
    int sizeoflist = searchList.Count;
    System.Console.WriteLine(sizeoftable);
    System.Console.WriteLine(searchList);
    return searchList;
}

我需要用 Key 迭代列表中的每个项目(具有哈希表)..

我正在使用 Foreach 进行迭代,但我没有获得传递键值的选项..

List<Hashtable> searchList = class1.StoreSearchResult(ngdriver);

foreach (var item in searchList)
{
  Hashtable tablevalue= item;
}

你能帮帮我吗?

【问题讨论】:

    标签: c# hashtable


    【解决方案1】:

    您的意思是循环所有哈希表元素吗?如果是,像这样:

            List<Hashtable> source = new List<Hashtable>();
            foreach (Hashtable hashtable in source)
            {
                foreach (object key in hashtable.Keys)
                {
                    var value = hashtable[key];
                }
            }
    

    【讨论】:

    • 我在拥有多行数据时遇到另一个错误。 System.ArgumentException: 项目已被添加。字典中的键:'ID' 正在添加的键:'ID'... 而不是 table.Add 我必须使用 put 选项??
    • 您正在尝试添加两个相同的键,而哈希表不允许这样做。你可以做类似 if (hashtable.ContainsKey("ID")) hashtable["ID"] = value;否则 hashtable.Add("ID", value)。如果您需要管理多个相同的行,您可以将更多的哈希表添加到 searchList 列表中。
    • 我必须像这样存储 Hashtable。Java 将允许我使用 put 方法。{ID=11, Type="Retail"} {ID="12", Type="WholeSale"} {ID="13", 类型="在线"}。我确实有多行.. 在 Java 中,如果我传递索引和键,它将返回值.. C# 中有什么东西吗?
    • 如果你总是使用 ID 作为键,你可以使用那个值作为键: hashtable.Add(11, "Retail"); hashtable.Add(12, "WholeSale"): ecc.抄送。
    • 这样我就不能用Key作为ID了吧?我必须将密钥传递为 11、12..??
    【解决方案2】:

    通常类似于hashtable[key]。即返回哈希表中的值,你也可以设置像hashtable[key] = value这样的值

    关于你的例子:

    List<Hashtable> searchList = class1.StoreSearchResult(ngdriver);
    
    foreach (var item in searchList)
    {
        Console.WriteLine(item[key]);
    }
    

    如果你想遍历哈希表:

    List<Hashtable> searchList = class1.StoreSearchResult(ngdriver);
    
    foreach (var item in searchList)
    {
       foreach (var entry in item) {
           Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
       }
    }
    

    看看this

    【讨论】:

      【解决方案3】:

      你可以试试这个:

        foreach (Hashtable hb in searchList)
              {
                  foreach (DictionaryEntry entry in hb)
                  {
                      Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
                  }
              }
      

      【讨论】:

        【解决方案4】:
        using System;
        using System.Collections;
        using System.Collections.Generic;
        
        namespace SOFAcrobatics
        {
            public static class Launcher
            {
                public static void Main ()
                {
                    Hashtable french_numbers = new Hashtable ();
                    french_numbers ["zero"] = 0;
                    french_numbers["cinq"] = 5;
        
                    Hashtable english_numbers = new Hashtable();
                    english_numbers ["zero"] = 0;
                    english_numbers ["five"] = 5;
        
                    Console.WriteLine(Launcher.SearchHashTablesByKey (new List<Hashtable> () {
                        french_numbers,
                        english_numbers
                    }, "five"));
        
                    Console.ReadKey(true);
                }
        
                private static Object SearchHashTablesByKey (List<Hashtable> list, Object key)
                {
                    foreach (Hashtable table in list)
                    {
                        if (table.ContainsKey(key))
                        {
                            return table[key];
                        }
                    }
                    return null;
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-17
          • 1970-01-01
          • 2020-02-20
          • 2020-09-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多