我有一个字典,将从查询的结果填充。由于这个原因,我不知道什么数据值会在我初始化时进入字典(尽管显然我知道将使用什么数据类型)。我是C#的新手 - 我怎么设置它?C#初始化字典,然后添加到它
伪代码,该字典结构我想要的是:
{
"visa": [2.75, 3.33],
"mastercard": [1.00, 4.32],
...
}
这是我到目前为止,但它不是编译:
//initialize the dictionary but do not populate yet
Dictionary<string, List<decimal>> cardtype_total_amount;
//simulate getting the first card type from the db
string cardtype = "visa";
//initialize the "visa" key
if (!cardtype_total_amount.ContainsKey(cardtype)) cardtype_total_amount.Add(cardtype, new List<decimal>(){0, 0});
//simulate updating the values for "visa" from the db (this would happen lots of times for each card type):
cardtype_total_amount[cardtype][0] += 0.5;
cardtype_total_amount[cardtype][1] += 1.7;
//add more keys for other cardtypes, and update their totals as per above...
你知道**有关数据的任何事吗?例如,它会总是由一个字符串和两个浮点组成? –
你是什么意思“失败”?它抛出异常吗? –
@KonradViltersten是数据来自SQL Server,并且将始终是一个字符串和两个“钱”型变量。 – mulllhausen