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

C# 将多个对象作为值添加到字典中的键,c#,dictionary,C#,Dictionary,我的任务是读取CSV文件,并将文件内容放入字典 问题是,正在读入的每一行都有一个字典中已经存在的键。我被告知将作为值(从类对象)读入的每一行添加到与该行(即帐户)相关的键中 因此,文件示例在每行上都是这样的,大约有10个帐户,但有8000行: 账户1、价值1、价值2、价值3 账户1、价值1、价值2、价值3 因此,我被要求做的是,当已经存在一个不同的键时,我需要将对象(读入的每一行)添加到该值中 但是当我被要求对一个物体做这件事时,我真的很困惑。我理解字典的方式是,如果键/值对是这样的(strin

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

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

因此,文件示例在每行上都是这样的,大约有10个帐户,但有8000行:

账户1、价值1、价值2、价值3

账户1、价值1、价值2、价值3

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

但是当我被要求对一个物体做这件事时,我真的很困惑。我理解字典的方式是,如果键/值对是这样的(string,int):

按键1,5

按键1,10

当我将第二个值添加到distinct键时,它将如下所示: 按键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);
            }
        }

    }
static void Main(字符串[]args)
{
双dParseSettleMM;
int iParseOpen;
国际iParseBuy;
国际Iparsell;
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\u 2016\u 02\u 25.0415.csv”;
Dictionary vSummaryResults=新字典();
//设置控件以在已排序列表之间切换。
Console.WriteLine(“输入1按帐户排序,输入2按基础排序,输入3按帐户和基础排序”);
string control=Console.ReadLine();
//开放式阅读器
StreamReader r=新的StreamReader(路径);
StreamWriter vWriteFile=新StreamWriter(“Positions2.csv”);
//消费一线
r、 ReadLine();
//While循环使用记录对象填充列表
而(!r.EndOfStream)
{
DataRecord记录=新的DataRecord();
var line=r.ReadLine();
var值=行分割(',');
//需要添加到字典中。。。
if(vSummaryResults.ContainsKey(records.account))
{
vSummaryResults[records.account].open+=records.open;
}
其他的
{
添加(records.account,records);
}
}
}
编辑:以下是我的具体问题。我被告知这段代码已经可以工作了,我试图理解的是如何和为什么,并试图将其可视化以理解它


当我将该类中的每个字段添加到字典中时,会发生什么?作为值使用的我的DataRecord类是否仅具有这些字段的多个实例?

将字典值设置为DataRecords的列表(或其他集合)

Dictionary<string, List<DataRecord>> vSummaryResults = new Dictionary<string, List<DataRecord>>();
Dictionary vSummaryResults=newdictionary();
然后,您可以将在执行期间找到的任何新值添加到该列表中

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

}
static void Main(字符串[]args)
{
双dParseSettleMM;
int iParseOpen;
国际iParseBuy;
国际Iparsell;
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\u 2016\u 02\u 25.0415.csv”;
Dictionary vSummaryResults=新字典();
//设置控件以在已排序列表之间切换。
Console.WriteLine(“输入1按帐户排序,输入2按基础排序,输入3按帐户和基础排序”);
string control=Console.ReadLine();
//开放式阅读器
StreamReader r=新的StreamReader(路径);
StreamWriter vWriteFile=新StreamWriter(“Positions2.csv”);
//消费一线
r、 ReadLine();
//While循环使用记录对象填充列表
而(!r.EndOfStream)
{
DataRecord记录=新的DataRecord();
var line=r.ReadLine();
var值=行分割(',');
//需要添加到字典中。。。
if(vSummaryResults.ContainsKey(records.account))
{
vSummaryResults[records.account]。添加(记录);
}
其他的
{
添加(records.account,new List());
vSummaryResults[records.account]。添加(记录);
}
}
}
选项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));
        }
    }

}
static void Main(字符串[]args)
{
双dParseSettleMM;
int iParseOpen;
国际iParseBuy;
国际Iparsell;
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\u 2016\u 02\u 25.0415.csv”;
Dictionary vSummaryResults=新字典();
//设置控件以在已排序列表之间切换。
Console.WriteLine(“输入1按帐户排序,输入2按基础排序,输入3按帐户和基础排序”);
string control=Console.ReadLine();
//开放式阅读器
StreamReader r=新的StreamReader(路径);
StreamWriter vWriteFile=新StreamWriter(“Positions2.csv”);
//消费一线
r、 ReadLine();
//While循环使用记录对象填充列表
var serializer=新的JavaScriptSerializer();