0

I'm using the CsvHelper library tool to help write lists that I've created to CSV file.

using (var sr = new StreamReader(inPath))
{
    using (var sw = new StreamWriter(outPath))
    {
        var reader = new CsvReader(sr);
        var writer = new CsvWriter(sw);

        IEnumerable records = reader.GetRecords<DataRecord>().ToList();

        List<CountAndFrequencyClass> list1 = new List<CountAndFrequencyClass>();
        list1 = CountAndFrequency(records, "ShipperName", 1);

        List<CountAndFrequencyClass> list2 = new List<CountAndFrequencyClass>();
        list2 = CountAndFrequency(records, "ShipperCity", 1);

        list1 = list1.Concat(list2).ToList();

        writer.WriteRecords(list2);
    }
}

The list1=list1.Concat(list2).ToList(); does indeed concatenate the strings, but it stacks them on top of each other when they're written out to the CSV file. I want to find a way to concatenate the lists horizontally (so they're displayed next to eachother) instead of vertically.

Thanks for any help and please let me know if additional information is needed!

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
jbove2
  • 5
  • 2
  • I don't know if you care about efficiency, but it seems like it would be a lot more efficient to loop through the rows of the CSV one-by-one, rather than choosing a column, looping through all the rows, choosing another column, looping through all the rows, etc. – ProgrammingLlama Jan 14 '20 at 02:09
  • Do you mean to say `append` a `list2` to `list1` and then write it as a single record to csv file – Prasad Telkikar Jan 14 '20 at 02:22
  • You can merge the lists using [Zip](https://stackoverflow.com/questions/5122737/what-is-the-use-of-enumerable-zip-extension-method-in-linq), but how are you going to store the resulting list? You'll need a class that is capable of holding the data from both lists side by side, i.e. has twice as many properties as `CountAndFrequencyClass`. – John Wu Jan 14 '20 at 02:29
  • @PrasadTelkikar, I have tried to use list1.AddRange(list2); to append the lists already, but this results in the same problem. The two lists are displayed vertically instead of horizontally. – jbove2 Jan 14 '20 at 02:30
  • @JohnWu That seems very promising. I'll use that in conjunction w/ ExpandoObject.Thanks a lot! – jbove2 Jan 14 '20 at 02:33

2 Answers2

1

You can use a loop to add list2 column to records of list1

foreach (var i=0 ; i< list1.Count; i++)
{
    if(i>=list2.Count ) break;
    var rec1 = list1[i];
    var rec2 = list2[i];
    rec1.NewColumn = rec2.ColumnToAdd;
}
Ali Hamidi
  • 61
  • 1
  • 2
  • 5
0

You could write them to the file as you loop through the lists.

using (var sr = new StreamReader(inPath))
{
    using (var sw = new StreamWriter(outPath))
    {
        var reader = new CsvReader(sr);
        var writer = new CsvWriter(sw);

        IEnumerable records = reader.GetRecords<DataRecord>().ToList();

        List<CountAndFrequencyClass> list1 = new List<CountAndFrequencyClass>();
        list1 = CountAndFrequency(records, "ShipperName", 1);

        List<CountAndFrequencyClass> list2 = new List<CountAndFrequencyClass>();
        list2 = CountAndFrequency(records, "ShipperCity", 1);

        for (int i = 0; i < list1.Count; i++)
        {
            writer.WriteRecord(list1[i]);
            writer.WriteRecord(list2[i]);
            writer.NextRecord();
        }
    }
}
David Specht
  • 7,784
  • 1
  • 22
  • 30