C#, Lambdas / Anonomous delegates

List Exporter To CSV

Today I wrote a small utility class that allows any List<T> to be exported to a CSV string or a CSV file using a nice Fluent interface.

This is what it allows:

  1. Take any List<T>, where T could be any class
  2. Let the using code specify what columns should be exported
  3. Use a Fluent API (as they are all the rage these days
  4. Allow auto header named to be obtained using using Expression trees
  5. Allow the the user to supply custom header names
  6. Allow the user to also supply custom format strings for the data being exported
  7. Allow the data to be exported to a CSV file OR allow the data to be exported to a CSV string

 

And here is an example of how you might use it

//Get it as a String result, using automatic Headers, but formatted Columns, 
//and standard "," seperator
String resultsWithDefaultHeadersAndFormattedColumns = people.GetExporter()
    .AddExportableColumn((x) => x.Age, 
                  customFormatString:"The Person Age Is {0}")
    .AddExportableColumn((x) => x.Name, 
                  customFormatString: "The Persons Name {0}")
    .AsCSVString();

If you want to know more you can read the full article over at :

http://www.codeproject.com/KB/miscctrl/CSVListExporter.aspx