Writing an IEnumerable
Hi Josh,
I am trying but failing to output a simple IEnumerable into the CSV. Eg:
public class UserEditViewModel
{
public IEnumerable<int> SelectedCategories { get; set; }
}
public sealed class UserExportModel : CsvClassMap<UserEditViewModel>
{
public UserExportModel()
{
Map(m => m.SelectedCategories)
}
}
The expected output in the CSV would be a single cell containing:
"1,5,2,24"
I have tried various combinations of ConvertUsing(), attempting to craft a Type Converter and just outputting dummy text in a custom ConvertUsing() but am always greeted with the error:
"Converting IEnumerable types is not supported for a single field. If you want to do this, create your own ITypeConverter and register it in the TypeConverterFactory by calling AddConverter."
Could you please provide an example of how to convert a simple enumeration into a comma-separated single cell?
I had also hoped that using lambda expressions I could map a single property of an enumeration to a single-cell comma separated list but could not get that to work either. Eg:
Map(m => m.StatesList.SelectMany(e => e.Name));
I would have hoped that this would output:
"NSW,QLD, VIC, SA"
But the error I receive is:
"Not a member access Parameter name: expression"
Thanks Simon
I would also like to have a working example. In my case I have a list of strings that I want to convert to a string with newlines. All the string would be a single cell with multiple lines in it.
Any chance there is an update on this?
For this to be a feature in the writer, it also needs to be in the reader. I spent around 8 hours last night looking into this. I came up with a mapping like this:
Map( m =>m .MyList1 ).IndexRange( 2, 4 );
Map( m => m.MyList2 ).IndexRange( 5 );
The first option would use indices 2, 3, and 4 and convert those fields into a list, or in the case of the writer, output to those field positions. Using just 5 means it starts at index 5 and will use all the remaining fields.
It will work with types that implement IList and IList<>. That means you can also use IList and IList<> as the property type.
I don't really like the implementation though. It's not very flexible at all. I think I may want to change the overall structure of how this works. Pushing all the work into the type converters would be the easiest, but they would need to drastically change. They would need more information passed to them, including the current IReaderRow. This would make it so you could create type converters for any type of collection you want, or any other type that requires more than a single field to be created.
I need to see if there are any other options that make sense for doing this also.
I just pushed version 3.0.0.0-beta1 to nuget and it contains the ability to read and write the most common IEnumerable types.
- IList
- ICollection
- IEnumerable
- ArrayList
- Array
- List
- Collection
- IList
- ICollection
- IEnumerable
I implemented it by making a lot of changes that allowed the converters to do the work. Converters now have access to ICsvReaderRow, ICsvWriterRow and CsvPropertyMapData.
The beta will more than likely have breaking changes every release. The changes will only affect you if you're doing some more custom stuff.
https://github.com/JoshClose/CsvHelper/commit/48dde9a730c222f3e6973de94e17eabdf657e37e
I just did an upgrade and even with the simplest data model I am getting a blank csv file -- I have even tried just writing the header and that doesn't even work. Not sure why --any thing I can do to troubleshoot? Right now there are no exceptions being thrown at all just nothing is written.
I changed the CsvWriter. Before, calling WriteRecord<T>( T record ) would call NextRecord(). I removed this call. This enabled more writing on that record to happen. A WriteField or another WriteRecord call could still be made. This also makes it consistent with how the CsvReader works. You Read(), then you can call GetField or GetRecord as many times as needed.
What are your thoughts on the change? It makes sense to me. ;)
the default behaviour for a list"apple,banana,orange,lemon, not to a set of fields for each string.
You are the only one to ask for it in this form so far. You should be able to do that trivially with ConvertUsing though.
Just to throw my $0.02 in...
I'm converting some json with an unknown structure to CSV using DataTable as an intermediate format (just in the playing around / R&D phase currently). I could just parse the JObject myself and format as I go, but it makes sense to me that a collection type would automatically be formatted as a single column of values separated with some delimiter. I can also just check the DT's row type.
As I don't know the structure up front I'm unable to use a mapper.