我的任务是读取 CSV 文件并将文件的内容放入字典中。
问题是读入的每一行都有一个已经存在于字典中的键。我被告知将读入的每一行作为值(从类对象)添加到与该行相关的键中,即帐户。
因此文件示例在每一行中看起来像这样,大约有 10 个帐户,但有 8,000 行:
帐户 1、值 1、值 2、值 3
帐户 1、值 1、值 2、值 3
所以我被要求做的是,当已经存在一个不同的键时,我需要将对象(读入的每一行)添加到该值中。
但是当我被要求对一个对象执行此操作时,这真的让我感到困惑。我理解字典的方式是,如果键/值对是这样的 (string, int):
键 1, 5
键 1, 10
当我将第二个值添加到不同的键时,它将如下所示: 键 1, 15
当谈到使用像 int 这样的简单值时,我完全可以理解这一点。
但是如果我使用类作为我的值:
public class DataRecord
{
public string account;
public int open;
public int buy;
public int sell;
public double settleMM;
public string underlying;
public string symbol;
}
到目前为止,这是我的应用程序:
static void Main(string[] args)
{
double dParseSettleMM;
int iParseOpen;
int iParseBuy;
int iParseSell;
string sUnderlyingControl = string.Empty;
string sUnderlying = string.Empty;
string sAccount = string.Empty;
string sAccountControl = string.Empty;
var path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReaderList\Position_2016_02_25.0415.csv";
Dictionary<string, DataRecord> vSummaryResults = new Dictionary<string, DataRecord>();
//Sets up control to switch between sorted lists.
Console.WriteLine("Enter 1 to sort by Account, 2 to sort by Underlying, 3 to sort by Account and Underlying");
string control = Console.ReadLine();
//open reader
StreamReader r = new StreamReader(path);
StreamWriter vWriteFile = new StreamWriter("Positions2.csv");
//Consume first line
r.ReadLine();
//While loop to populate List with record Objects
while (!r.EndOfStream)
{
DataRecord records = new DataRecord();
var line = r.ReadLine();
var values = line.Split(',');
//Need to add into dictionary...
if (vSummaryResults.ContainsKey(records.account))
{
vSummaryResults[records.account].open += records.open;
}
else
{
vSummaryResults.Add(records.account, records);
}
}
}
编辑:这是我的具体问题。有人告诉我这段代码已经可以工作了,我想了解的是工作方式和原因,并试图将其形象化以理解它。
当我将该类添加到字典时,该类中的每个字段到底发生了什么?用作值的我的 DataRecord 类是否只有这些字段的多个实例?
最佳答案
使您的字典值成为 DataRecords 的列表(或其他集合)。
Dictionary<string, List<DataRecord>> vSummaryResults = new Dictionary<string, List<DataRecord>>();
然后您可以将在执行过程中发现的任何新值添加到该列表。
选项 1:我继续修改您的代码来执行此操作。
static void Main(string[] args)
{
double dParseSettleMM;
int iParseOpen;
int iParseBuy;
int iParseSell;
string sUnderlyingControl = string.Empty;
string sUnderlying = string.Empty;
string sAccount = string.Empty;
string sAccountControl = string.Empty;
var path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReaderList\Position_2016_02_25.0415.csv";
Dictionary<string, List<DataRecord>> vSummaryResults = new Dictionary<string, List<DataRecord>>();
//Sets up control to switch between sorted lists.
Console.WriteLine("Enter 1 to sort by Account, 2 to sort by Underlying, 3 to sort by Account and Underlying");
string control = Console.ReadLine();
//open reader
StreamReader r = new StreamReader(path);
StreamWriter vWriteFile = new StreamWriter("Positions2.csv");
//Consume first line
r.ReadLine();
//While loop to populate List with record Objects
while (!r.EndOfStream)
{
DataRecord records = new DataRecord();
var line = r.ReadLine();
var values = line.Split(',');
//Need to add into dictionary...
if (vSummaryResults.ContainsKey(records.account))
{
vSummaryResults[records.account].Add(record);
}
else
{
vSummaryResults.Add(records.account, new List<DataRecord>());
vSummaryResults[records.account].Add(record);
}
}
}
选项 2:因为您不能使用选项 1。这将序列化每条记录并将其附加到键的值。然后你可以用','拆分它并反序列化它。上面的方法比较好,但是如果你不能使用它,那么这个应该可以。
static void Main(string[] args)
{
double dParseSettleMM;
int iParseOpen;
int iParseBuy;
int iParseSell;
string sUnderlyingControl = string.Empty;
string sUnderlying = string.Empty;
string sAccount = string.Empty;
string sAccountControl = string.Empty;
var path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReaderList\Position_2016_02_25.0415.csv";
Dictionary<string, string> vSummaryResults = new Dictionary<string, string>();
//Sets up control to switch between sorted lists.
Console.WriteLine("Enter 1 to sort by Account, 2 to sort by Underlying, 3 to sort by Account and Underlying");
string control = Console.ReadLine();
//open reader
StreamReader r = new StreamReader(path);
StreamWriter vWriteFile = new StreamWriter("Positions2.csv");
//Consume first line
r.ReadLine();
//While loop to populate List with record Objects
var serializer = new JavaScriptSerializer();
while (!r.EndOfStream)
{
DataRecord records = new DataRecord();
var line = r.ReadLine();
var values = line.Split(',');
//Need to add into dictionary...
if (vSummaryResults.ContainsKey(records.account))
{
vSummaryResults[records.account] += "," + serializer.Serialize(record);
}
else
{
vSummaryResults.Add(records.account, serializer.Serialize(record));
}
}
}
关于c# - 将多个对象作为值添加到字典中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35757134/