C# Specflow中的多行和多列

C# Specflow中的多行和多列,c#,selenium,cucumber,specflow,C#,Selenium,Cucumber,Specflow,我有一个Specflow表,看起来像这样 When I Perform POST Operation for "/example/" with body | answerValue1 | answerValue2 | countryCode | Cash | | Yes | Yes | AD | 76-100% | | | | AF |

我有一个Specflow表,看起来像这样

 When I Perform POST Operation for "/example/" with body
 | answerValue1  | answerValue2 | countryCode | Cash    |
 | Yes           | Yes          | AD          | 76-100% |  
 |               |              | AF          |         |
CountryCode列是唯一可以多选的列。 我试着用一个简单的tableExtenstions将这些列添加到字典中

 public class TableExtensions
        {
            public static Dictionary<string, string> ToDictionary(Table table)
            {
                var dictionary = new Dictionary<string, string>();
                foreach (var row in table.Rows)
                {
                    dictionary.Add(row[0], row[1]);
                }
                return dictionary;
            }
      }
不幸的是,我得到错误字典中不存在给定的键, 因为字典只从第一个键和第二个键返回两个值

当然,如果我将键更改为
row[2],row[3]
它将得到正确的列。 但是我想重用表扩展

我还试图增加它们,但只需要第一列就可以了

 var i = 0;
                foreach (var row in table.Rows)
                {
                    dictionary.Add(row[i], row[i]);
                    i++;
                }

有谁有更好的解决方案吗?

我不完全确定您希望字典最终包含什么,但正如您提到的,手动更改它所查找的行可以:

row[2], row[3]
提供您想要的数据,也许这将为您提供所需的可重用性:

 public class TableExtensions
    {
        public static Dictionary<string, string> ToDictionary(Table table, int columnOne, int columnTwo)
        {
            int i = 0;

            var dictionary = new Dictionary<string, string>();
            foreach (var row in table.Rows)
            {
                dictionary.Add(row[columnOne], row[columnTwo]);
            }
            return dictionary;
        }
    }
这将生成包含以下内容的词典:

您可以得到如下国家代码:

foreach (var row in dictionary)
{
    var countryCode = row.Key;
    var score = row.Value ?? string.empty;
}  

我不完全确定您希望词典最终包含什么,但正如您所提到的,手动将其查找的行更改为:

row[2], row[3]
提供您想要的数据,也许这将为您提供所需的可重用性:

 public class TableExtensions
    {
        public static Dictionary<string, string> ToDictionary(Table table, int columnOne, int columnTwo)
        {
            int i = 0;

            var dictionary = new Dictionary<string, string>();
            foreach (var row in table.Rows)
            {
                dictionary.Add(row[columnOne], row[columnTwo]);
            }
            return dictionary;
        }
    }
这将生成包含以下内容的词典:

您可以得到如下国家代码:

foreach (var row in dictionary)
{
    var countryCode = row.Key;
    var score = row.Value ?? string.empty;
}  

考虑到国家代码的简单性,我会将其设置为逗号分隔的列表,并使用垂直表格:

When I Perform POST Operation for "/example/"
    | Field        | Value  |
    | answerValue1 | ...    |
    | answerValue2 | ...    |
    | countryCodes | AD, AF |
    | cash         | ...    |
在步骤定义中:

var example = table.CreateInstance<ExampleRow>();

// use example.GetCountryCodes();

考虑到国家代码的简单性,我会将其设置为逗号分隔的列表,并使用垂直表格:

When I Perform POST Operation for "/example/"
    | Field        | Value  |
    | answerValue1 | ...    |
    | answerValue2 | ...    |
    | countryCodes | AD, AF |
    | cash         | ...    |
在步骤定义中:

var example = table.CreateInstance<ExampleRow>();

// use example.GetCountryCodes();

请检查,请检查,这正是我要出去的地方。非常感谢你的帮助!这正是我所追求的。非常感谢你的帮助!