如何在具有Hashtable的List中获取key的值

时间:2016-04-18 10:17:01

标签: c# 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 ..迭代列表中的每个项目(具有Hashtable)。

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

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

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

你能帮帮我吗?

4 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

通常类似于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

答案 2 :(得分:0)

你可以试试这个:

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

答案 3 :(得分:0)

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