0

I have class with nested list properties, I am trying to write the value to CSV file, but I am getting output appended with [{ }] like shown below:

Client  TDeals
ABC     [{DealName:59045599,TShape:[{StartDate:"2014-01-
        28T23:00:00",EndDate:"2014-01-28T23:30:00",Volume:0.00},
        {StartDateTime:"2014-01-                
        28T23:30:00",EndDateTime:"2014-01-29T00:00:00",Volume:0.00}}]

I want my output in CSV file like shown below:

Client      DealNo        StartDate        EndDate         Volume
ABC        59045599         -                -               -

Class Properties

  public class TRoot
    {
            public string Client { get; set; }
            public List<TDeal> Deals { get; set; }
    }

  public class TDeal
  {
    public string DealName{get;set;}
    public List<TInterval> TShape { get; set; }
  }

  public class TInterval
  {
     public string StartDate{ get; set; }
     public string EndDate{ get; set; }
     public string Volume {get;set;}
  }

I am using ServiceStack.Text to create CSV file from object

ServiceStack.Text.CsvSerializer.SerializeToWriter<TRoot>(TRoot, writer);

Reference URL https://github.com/ServiceStack/ServiceStack.Text

3
  • If you want like "shown below", then write your custom code, or if the library, which you've used, not working - contact the developer/support of that library. But if you look closely to output example given in ServiceStack's github page, it is wrapped in [{}]. Commented Jan 17, 2018 at 10:22
  • @SeM is their any other library which will help in writing nested list to csv file Commented Jan 17, 2018 at 10:23
  • sorry, but I don't know. Commented Jan 17, 2018 at 10:27

3 Answers 3

3

Define a new class for single csv line:

public class CsvLine
{
    public string Client { get; set; }
    public string DealName { get; set; }
    public string StartDate { get; set; }
    public string EndDate { get; set; }
    public string Volume { get; set; }
}

Now you can transfrom your objects into collection of lines with Linq SelectMany method:

TRoot root = ...
var lines = root.Deals.SelectMany(d => d.TShape.Select(s => new CsvLine
{
    Client = root.Client,
    DealName = d.DealName,
    StartDate = s.StartDate,
    EndDate = s.EndDate,
    Volume = s.Volume
})).ToArray();

Then call SerializeToWriter on that collection

Sign up to request clarification or add additional context in comments.

Comments

2

I would recommend to "flatten" your output to CSV.

Create one more class that will be a mirror of what you would like to have in CSV file. Before writing to the file, convert your TRoot to that new class and write it to CSV.

Quite quick and elegant solution :)

Comments

0

You can try Cinchoo ETL to create the CSV file. First you will have to flatten out root object using Linq and pass them to CSV writer to create file.

Sample below show how to

private static void Test()
{
    TRoot root = new TRoot() { Client = "ABC", Deals = new List<TDeal>() };
    root.Deals.Add(new TDeal
    {
        DealName = "59045599",
        TShape = new List<TInterval>()
    {
        new TInterval { StartDate = DateTime.Today.ToString(), EndDate = DateTime.Today.AddDays(2).ToString(), Volume = "100" },
        new TInterval { StartDate = DateTime.Today.ToString(), EndDate = DateTime.Today.AddDays(2).ToString(), Volume = "200" }
    }
    });

    using (var w = new ChoCSVWriter("nestedObjects.csv").WithFirstLineHeader())
    {
        w.Write(root.Deals.SelectMany(d => d.TShape.Select(s => new { ClientName = root.Client, DealNo = d.DealName, StartDate = s.StartDate, EndDate = s.EndDate, Volume = s.Volume })));
    }

}

The output is:

ClientName,DealNo,StartDate,EndDate,Volume
ABC,59045599,1/17/2018 12:00:00 AM,1/19/2018 12:00:00 AM,100
ABC,59045599,1/17/2018 12:00:00 AM,1/19/2018 12:00:00 AM,200

For more information about it, visit the codeproject article at

https://www.codeproject.com/Articles/1155891/Cinchoo-ETL-CSVWriter

Disclaimer: I'm the author of this library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.