简体   繁体   English

将字典值添加为字符串列表

[英]Adding dictionary values as a list of strings

I am trying to create a dictionary where the values are stored as a list of strings that I can iterate over.我正在尝试创建一个字典,其中值存储为可以迭代的字符串列表。 The end goal being something I can mix and match values for passing into parameters in the method, for different Selenium tests.最终目标是我可以混合和匹配值以传递到方法中的参数,用于不同的 Selenium 测试。 Basically, the Dictionary is for storing test data.基本上,字典用于存储测试数据。

  public void EnterSearchInfo (string username, string cropname, string stationname, string applicationname)
{
    Dictionary<string, List<string>> searchValues = new Dictionary<string, List<string>>();
    searchValues.Add("User", ["USER1", "USER2"]);
    searchValues.Add("Crop", ["Corn", "Soy"]);
    searchValues.Add("Station", ["AA", "JH", "HW"]);


    _driver.FindElement(user).SendKeys(username);
    _driver.FindElement(crop).SendKeys(cropname);
    _driver.FindElement(station).SendKeys(stationname);
    _driver.FindElement(application).SendKeys(applicationname);
    _driver.FindElement(findAssocBtn).Click();
}

The above is not working however, specifically this piece (I am not adding the values correctly):但是,上面的方法不起作用,特别是这部分(我没有正确添加值):

searchValues.Add("User", ["USER1", "USER2"]);
searchValues.Add("Crop", ["Corn", "Soy"]);
searchValues.Add("Station", ["AA", "JH", "HW"]);

Any ideas?有任何想法吗? Or if there is a better way to handle this, please let me know或者如果有更好的方法来处理这个,请告诉我

You're just missing the instantiation for each list element of the dictionary.您只是缺少字典的每个列表元素的实例化。 If you know the values at the time of dictionary creation you can use:如果您知道创建字典时的值,则可以使用:

Dictionary<string, List<string>> searchValues = new Dictionary<string, List<string>>
{
    { "User", new List<string>() {"USER1", "USER2" } },
    { "Crop", new List<string>() {"Corn", "Soy" } },
    { "Station", new List<string>() {"AA", "JH", "HW" } }
};

Alternatively, if your values are unknown at time of dictionary creation ie when values are retrieved from a text file or database, you can use the Add method from List class.或者,如果您的值在创建字典时未知,即从文本文件或数据库中检索值时,您可以使用 List class 中的 Add 方法。

Dictionary<string, List<string>> searchValues = new Dictionary<string, List<string>>();
searchValues.Add("User", new List<string>() { "USER1", "USER2" });
searchValues.Add("Crop", new List<string>() { "Corn", "Soy" });
searchValues.Add("Station", new List<string>() { "AA", "JH", "HW" });    

This is what you are looking for GenerareSetLitere :这就是您要寻找的GenerareSetLitere

Dictionary<string, List<Piese>> SetPiese = new Dictionary<string, List<Piese>>
{
    { "User", GenerareSetLitere("USER1", "USER2") },
    { "Crop", GenerareSetLitere("Corn", "Soy") }
    { "Station", GenerareSetLitere("AA", "JH", "HW") }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM