将多个对象作为值添加到字典中的键

本文关键字:字典 添加 对象 | 更新日期: 2023-09-27 18:37:14

我的任务是从CSV文件中读取并将文件的内容放入字典中。

问题是正在读入的每一行都有一个字典中已经存在的键。 我被告知将读入值(来自类对象)的每一行添加到与该行相关的键中,即帐户。

所以文件示例在每一行上都是这样的,大约有 10 个帐户,但有 8,000 行:

帐户 1, 值 1, 值

2, 值 3

帐户 1, 值 1, 值

2, 值 3

因此,我被要求做的是,当已经存在一个不同的键时,我需要将对象(读入的每一行)添加到该值中。

但是当我被要求用一个对象来做这件事时,这真的让我感到困惑。 我理解字典的方式是,如果键/值对是这样的(字符串,整数):

键 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 类是否只有这些字段的多个实例?

将多个对象作为值添加到字典中的键

使字典值成为数据记录的列表(或其他集合)。

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));
        }
    }
}