![](/img/trans.png)
[英]How to convert List<Class> to Dictionary<string, List<String>> in C#?
[英]How to convert a SpecFlow table into a Dictionary<string, List<string>> c#
我必須遵循以下SpecFlow代碼:
And I get and validate parameters
| ParameterName| Value | Answers | Mandatory | Meta | Modified | ReadOnly | Submit | SubmitValues | Tag |
| SurName | | | true | | false | false | true | | input |
| Name | | | true | | false | false | false | | input |
.....
我希望此表將其轉換為Dictionary<string, List<string>>
,頭列將是鍵,其余信息將是值。 我對一些值進行了硬編碼:
Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
dictionary.Add("ParameterName", new List<string> {"SurName", "Name", "Age"});
dictionary.Add("Value", new List<string> { "", "", "" });
dictionary.Add("Answers", new List<string> { "", "", "" });
dictionary.Add("Mandatory", new List<string> { "true", "true", "true" });
dictionary.Add("Meta", new List<string> { "", "", "" });
dictionary.Add("Modified", new List<string> { "false", "false", "false" });
dictionary.Add("ReadOnly", new List<string> { "false", "false", "false" });
dictionary.Add("Submit", new List<string> { "true", "false", "true" });
dictionary.Add("SubmitValues", new List<string> { "", "", "" });
dictionary.Add("Tag", new List<string> { "input", "input", "select" });
但是實際表中有很多值,我需要對所有值進行此操作,並且它們可能會更改,這就是為什么我不需要硬編碼字典的原因。 我怎樣才能做到這一點?
在這樣的字典中,同一行的檢索非常麻煩(您應該為“值”列表建立索引)...
相反,我將創建一個List<TestParameters>
,其中TestParameters
類包含具有常規強類型屬性的行:
public class TestParameters
{
public string ParameterName { set; set; }
public int Value { set; set; }
public bool Mandatory { set; set; }
// etc.
}
因此,現在您可以在某個測試步驟中進行如下操作:
[Given(@"I get and validate parameters")]
public void GetParameters(Table parameters)
{
}
只需將Table
替換為您更具體的類型:
[Given(@"I get and validate parameters")]
public void GetParameters(List<TestParameters> parameters)
{
}
只需在助手類中定義Table-> List轉換步驟:
[Binding]
public class Transformations
{
[StepArgumentTransformation]
public List<TestParameters> GetTestParameters(Table table)
{
return table.Rows.Select(row => new TestParameters
{
// string prop
ParameterName = row["ParameterName"],
// int prop
Value = !String.IsNullOrEmpty(row["Value"]) ? Int32.Parse(row["Value"]) : 0,
// bool prop
Mandatory = row["Mandatory"]?.ToLowerInvariant() == "true"
// TODO: other properties
}).ToList();
}
}
當然,如果您真的堅持的話Dictionary<string, List<string>>
轉換的結果也可以是Dictionary<string, List<string>>
。
您還可以使用TechTalk.SpecFlow.Assist命名空間中的CreateSet擴展方法。
在這里查看文檔: http : //specflow.org/documentation/SpecFlow-Assist-Helpers/
小例子:
CreateSet是Table的擴展方法,它將表數據轉換為一組對象。 例如,如果您具有以下步驟:
Given these products exist
| Sku | Name | Price |
| BOOK1 | Atlas Shrugged | 25.04 |
| BOOK2 | The Fountainhead | 20.15 |
您可以將表中的數據轉換為一組對象,如下所示:
[Given(@"Given these products exist")]
public void x(Table table)
{
var products = table.CreateSet<Product>();
// ...
}
回答這個問題為時已晚,但可能會對其他人有所幫助。
處理集合中此類數據的更好方法應采用List>格式。
以下擴展方法應在這方面有所幫助-
public static List<Dictionary<string, string>> ConvertToDictionaryList(this Table dt)
{
var lstDict = new List<Dictionary<string, string>>();
if (dt!=null)
{
var headers = dt.Header;
foreach (var row in dt.Rows)
{
var dict = new Dictionary<string, string>();
foreach (var header in headers)
{
dict.Add(header, row[header]);
}
lstDict.Add(dict);
}
}
return lstDict;
}
但是,如果您仍然想使用Dictionary>,則可以使用以下代碼段
public static List<Dictionary<string, string>> ConvertToDictionaryList(this Table dt)
{
var lstDict = new Dictionary<string, List<string>>();
if (dt != null)
{
var headers = dt.Header;
foreach (var row in dt.Rows)
{
foreach (var header in headers)
{
if (lstDict.ContainsKey(header))
{
lstDict[header].Add(row[header]);
}
else
{
lstDict.Add(header, new List<string> { row[header] });
}
}
}
}
return lstDict;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.