1

Right now I have a simple application where I have a C# object that has a List<String> as part of its attributes. It is supposed to store a list of strings alongside the other attributes and output it as a .csv file with headers.

After creating the object and outputting it as a .csv file, the column containing the list is not in the output and I don't know how to exactly handle it or converting it to a .csv format.

My intended output is:

Name,ID,Date,Number,FilePaths,Remarks
Value1, Value2, Value3, Value 4,List here,Value6

The output .csv file will then be serialized into JSON and be deserialized in another component where it is going to read the values and eventually be put into a database.

The class (POCO):

class Object 
{
 public string Name { get; set; }
 public string ID { get; set; }
 public DateTime Date { get; set; }
 public long Number { get; set; }
 public List<String> FilePaths { get; set; }
 public string Messages {get; set; }
}

And here is how I instantiate the object from the data collected from inputs:

List<String> paths = new List<String>();
foreach(var file in files)
{
 paths.Add(file.FullName);
}
List<Object> newObject = new List<Object> {
new Object { Name = value1, ID = value2, Date = value3, Number = value4, FilePaths = paths, Messages = value6 };
using(var writer = new CsvWriter(outputStream))
{
 writer.WriteRecords(newObject);
}

However the output seems to be:

Name,ID,Date,Number,Remarks
value1,value2,value3,value4,value6

It seems to have skipped the list.

EDIT:

To elaborate further, the FilePaths are actually a list of strings that contain the directory paths of files the user has uploaded to the program. What I plan to do is to have the list of file paths retrieved from List<FileInfo> files by calling a for loop and storing their paths into an organized fashion. This is because one user has the ability to upload multiple files as according to the requirements.

asked Jun 19, 2019 at 3:27
5
  • Could you elaborate more on that? It's my first time using CsvHelper as a way to read/write .csvs. Commented Jun 19, 2019 at 5:27
  • Oh! I apologize, Header1 was just the example I thought of. The actual csv's headers are named as what it seems to be on the output. Commented Jun 19, 2019 at 5:33
  • Did @AD8's suggestion work? Commented Jun 19, 2019 at 6:00
  • @AD8's suggestion was my initial idea too, but the requirements did state that the program is supposed to output a .csv that would be the input to a json serializer, which then goes through a deserializer and finally be put into a database. I'm supposed to keep the file paths in an array/list<string> and I'm having a hard time figuring out the solution. Commented Jun 19, 2019 at 6:28
  • Are you sure the output has Remarks? I am pretty sure it would be Messages. Also, Name, ID and so on are called "Properties" not "Attributes". Attributes in c# are something totally different. ADT's answer should solve your issue. Commented Jun 19, 2019 at 12:09

3 Answers 3

1

Declare FilePaths as string. Convert List as the string you want and And then write to csv file.

answered Jun 19, 2019 at 3:52
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the prompt reply, so basically I'll have to convert the class attribute as public String FilePaths { get; set; } instead?
That's one way to go. But once you are using Json, why do you need to write csv ? Why don't you convert the class to Json directly?
It's part of the specifications for the homework, I was stuck on this part for a rather long time.
1

Try initializing your list of object as the following:

List<Object> newObject = new List<Object> {
 new Object {
 Name = value1,
 ID = value2,
 Date = value3,
 Number = value4,
 FilePaths = paths.Any() ? $"\"{string.Join(",", paths.ToList())}\"" : null;,
 Messages = value6
 }
 };
mjwills
24.1k6 gold badges44 silver badges74 bronze badges
answered Jun 19, 2019 at 4:42

2 Comments

I suspect the Any check is not necessary.
@mjwills, you may be right, but I didn't have setup to test this and wanted to make sure it doesn't break. Hence why I left that in. Thanks for pointing that out.
0

CsvHelper skips over Enumerable properties unless you give it more information on how it should handle them. This would be one way to output your CSV file by separating each FilePath value with a comma.

public static void Main(string[] args)
{
 var paths = new List<string> { "path1", "path2", "path3" };
 List<Object> newObject = new List<Object> {
 new Object
 {
 Name = "NameValue",
 ID = "IdValue",
 Date = DateTime.Now,
 Number = 12345,
 FilePaths = paths,
 Messages = "MessagesValue"
 }
 };
 using (var csv = new CsvWriter(outputStream))
 {
 csv.Configuration.RegisterClassMap(new ObjectMap());
 csv.WriteRecords(newObject);
 }
}
public class ObjectMap : ClassMap<Object>
{
 public ObjectMap()
 {
 AutoMap();
 Map(m => m.FilePaths).TypeConverter<ListStringConverter>();
 }
}
private class ListStringConverter : StringConverter
{
 public override string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData)
 {
 var returnValue = string.Empty;
 var list = (List<string>)value;
 if (list != null)
 {
 returnValue = string.Join(",", list);
 } 
 return base.ConvertToString(returnValue, row, memberMapData);
 }
 public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
 {
 var list = text.Split(',').ToList();
 return list;
 }
}
public class Object
{
 public string Name { get; set; }
 public string ID { get; set; }
 public DateTime Date { get; set; }
 public long Number { get; set; }
 public List<String> FilePaths { get; set; }
 public string Messages { get; set; }
}
answered Jun 19, 2019 at 17:43

1 Comment

Thank you so much David, now I understand how it works and this solution worked. I had trouble understanding how it works initially because I've only worked with types like int string and stuff. Really is interesting to see how to make it work with Enumerable collections!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.