如何获取具有哈希表的列表中的键值

How to get the value of key in List which has Hashtable

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;
}

你能帮帮我吗?

你是说循环所有哈希表元素吗?如果是,像这样:

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

一般是hashtable[key]。即 returns 散列 table 中的值,您也可以设置 hashtable[key] = value

这样的值

我对你的榜样表示敬意:

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

foreach (var item in searchList)
{
    Console.WriteLine(item[key]);
}

如果要遍历哈希table:

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

你可以试试这个:

  foreach (Hashtable hb in searchList)
        {
            foreach (DictionaryEntry entry in hb)
            {
                Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
            }
        }
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;
        }
    }
}