Using FileHelpers, I decorated a class with [DelimitedRecord(",")] and was going to output an enumeration of objects of that type as CSV. But, it didn’t work because my class inherits from ActiveRecordLinqBase<T>, which caused some problems.
So, I was wondering if I could just select an enumeration of anonymous types and somehow have filehelpers generate csv from that. I don’t like having to define a class just for FileHelpers to output csv.
I would be open to using another csv library, but FileHelpers is proven.
EDIT
@Foovanadil: This would be the sort of thing I am trying to do:
CreateCSV(MyCollection.Select(x=>new{
x.Prop1,
x.Prop2,
x.Prop3
}));
Gives you:
Prop1,Prop2,Prop3
val1a,val2a,val3a
val1b,val2b,val3b
etc.....
LINQ To CSV worked great for me.
Here’s an example of how I am using it:
UPDATE
There are some downsides to the approach above:
So, I decided I would create a class just for the CSV records, which didn’t end up being any more work than the anonymous type did. I used Auto Mapper to flatten my source class and populate the property values of the CSV class. I also decided to compare FileHelpers to Linq To CSV. Linq To CSV was the obvious winner, in my situation:
I hope these findings are useful. Here is my new code: