Skip to content
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

Closed
CallMeBruce opened this issue May 15, 2015 · 5 comments
Closed

Writing a collection that contains collections #389

CallMeBruce opened this issue May 15, 2015 · 5 comments

Comments

@CallMeBruce
Copy link

Hi, is it possible to write a collection that contains collections, e.g. parent record, and child records?

--> Parent1, Name, Age
-->--> Child1, Field1, Field2
-->--> Child2, Field1, Field2
--> Parent2, Name, Age
-->--> Child3, Field1, Field2
...

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

@jamesbascle
Copy link
Contributor

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/

@JoshClose
Copy link
Owner

Enumerable properties are supported in version 3.0.

@Jerome2606
Copy link

Can you provide example to add row for each nested object ? (I'm ok to use version 3.0 but how ?)

@JoshClose
Copy link
Owner

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

@jzabroski
Copy link

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);
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants