-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writing a collection that contains collections #389
Comments
You cannot have nested collections as far as I know. You CAN have nested objects however. See reference maps just under the AutoMapping section @ http://joshclose.github.io/CsvHelper/ |
Enumerable properties are supported in version 3.0. |
Can you provide example to add row for each nested object ? (I'm ok to use version 3.0 but how ?) |
void Main()
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader))
{
writer.WriteLine("Id,Name,A,B,C");
writer.WriteLine("1,one,a,b,c");
writer.WriteLine("2,two,d,e,c");
writer.Flush();
stream.Position = 0;
csv.Configuration.RegisterClassMap<TestMap>();
csv.GetRecords<Test>().ToList().Dump();
}
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public List<string> ExtraStrings { get; set; }
}
public sealed class TestMap : CsvClassMap<Test>
{
public TestMap()
{
Map( m => m.Id );
Map( m => m.Name );
Map( m => m.ExtraStrings ).Index( 2 );
}
} You can also take a look at the unit tests if you like. https://github.com/JoshClose/CsvHelper/tree/master/src/CsvHelper.Tests/TypeConversion |
Updated working example for 19.0.0 👍 void Main()
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader, System.Globalization.CultureInfo.CurrentCulture))
{
writer.WriteLine("Id,Name,A,B,C");
writer.WriteLine("1,one,a,b,c");
writer.WriteLine("2,two,d,e,c");
writer.Flush();
stream.Position = 0;
csv.Configuration.RegisterClassMap<TestMap>();
csv.GetRecords<Test>().ToList().Dump();
}
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public List<string> ExtraStrings { get; set; }
}
public sealed class TestMap : ClassMap<Test>
{
public TestMap()
{
Map(m => m.Id);
Map(m => m.Name);
Map(m => m.ExtraStrings).Index(2);
}
} |
Hi, is it possible to write a collection that contains collections, e.g. parent record, and child records?
I thought that the AutoMapper might do this via it's recursion, however when I tried it using
WriteRecords(items)
however only the parent record was written.Thanks
The text was updated successfully, but these errors were encountered: