Search code examples
c#.netfilehelpers

Convert a List<T> to csv file using FileHelpers


I have a class like so

public class MyObject
{
    public string Name { get; set; }
    public string Location {get; set; }
}

This is then converted to a list of the type above with data. I want to use filehelpers to convert my list object to a csv string.

Currently I am doing this

    List<MyObject> objList = new List<MyObject>();

    //populate objList here
    //..
    //..

    var feng = new FileHelperEngine<List<MyObject>>();
    string str1 = feng.WriteString(new MyObject[]  { objList });

This is giving me an error

Argument 1: cannot convert from 'MyObject[]' to
 'System.Collections.Generic.IEnumerable<System.Collections.Generic.List<MyObject>' 

The other error is :

Cannot implicitly convert type
'System.Collections.Generic.List<MyObject>'
to 'MyObject'

How do I fix this?


Solution

  • Instead of this:

    var feng = new FileHelperEngine<List<MyObject>>();
    string str1 = feng.WriteString(new MyObject[]  { objList });
    

    You want this:

    var feng = new FileHelperEngine<MyObject>();
    string str1 = feng.WriteString(objList);