Replies: 4 comments
-
Are there 2 data structures or 2 maps or both? |
Beta Was this translation helpful? Give feedback.
-
@bazarniy - I have tried switching maps before and have found you cannot switch maps after you start reading records. The only thing I could come up with was to create another class that had both objects you were trying to get. public class Program
{
static void Main(string[] args)
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
writer.WriteLine("obj1_Test;obj2_Test");
writer.WriteLine("Test1;Test2");
writer.Flush();
stream.Position = 0;
csv.Configuration.Delimiter = ";";
csv.Configuration.RegisterClassMap(new TestMap("obj1","obj2"));
var records = csv.GetRecords<DataCombiner>().ToList();
}
}
}
internal sealed class TestMap : ClassMap<DataCombiner>
{
public TestMap(string obj1Prefix, string obj2Prefix)
{
Map(m => m.Data1.Test).Name(obj1Prefix + "_" + nameof(Data.Test));
Map(m => m.Data2.Test).Name(obj2Prefix + "_" + nameof(Data.Test));
}
}
public class DataCombiner
{
public Data Data1 { get; set; }
public Data Data2 { get; set; }
}
public class Data
{
public string Test { get; set; }
} |
Beta Was this translation helpful? Give feedback.
-
There are 2 same data structures with different headers in 1 row. public class Point
{
public int X { get; set; }
public int Y { get; set; }
} Csv:
I've made 2 mappers:
And there are no way to switch them on @AltruCoder thank you for your reply. Finally I have to inherit 2 types from base structure and make appropriate mappers: public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
public class Point1: Point { }
public class Point2: Point { }
public sealed class TestMap<T>: ClassMap<T> where T: Point
{
public TestMap(string prefix)
{
Map(m => m.Test).Name(prefix + "_" + nameof(Data.Test));
}
} var mapper1 = new TestMap<Point1>("point1");
var mapper2 = new TestMap<Point2>("point2");
using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.RegisterClassMap(mapper1);
csv.Configuration.RegisterClassMap(mapper2);
csv.Read();
csv.ReadHeader();
while (csv.Read())
{
var point1 = csv.GetRecord<Point1>();
var point2 = csv.GetRecord<Point2>();
}
} But I still think cozy way looks like this: var mapper1 = new TestMap<Point>("point1");
var mapper2 = new TestMap<Point>("point2");
using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.RegisterClassMap(mapper1);
csv.Configuration.RegisterClassMap(mapper2);
csv.Read();
csv.ReadHeader();
while (csv.Read())
{
var point1 = csv.GetRecord<Point>(mapper1); // can select already registered mapper in some way
var point2 = csv.GetRecord<Point>(mapper2); // can select already registered mapper in some way
}
} |
Beta Was this translation helpful? Give feedback.
-
Hello, does this have a fix, i run into the same problem, my use case is that I have a user DTO class and two class maps, one for when the DTO is used to create the user and one for when the DTO is used to update the user. When I read the csv base on one field I register the correct class map, but if my CSV contains one row for create and one for update after I unregister the previous map and register the correct one again, the initial is still used. I could create an exact copy of my DTO and probably will work but this looks like a weird bug. |
Beta Was this translation helpful? Give feedback.
-
Hi
I want to parse multiple objects with same type from one row.
I have data structure like this:
public class Data { public string Test {get; set;} }
This is csv headers format:
obj1_Test;obj2_Test
So I've created custom mapper for object 1 and object 2 and add initialized instances into configuration
And then I've found there are no arguments to select concrete mapper instance at GetRecord.
If it try RegisterClassMap with concrete instance before GetRecord and unregister after I'll get a bug: mappers doesn't reset and first mapper continues work.
I can create different data object types or I can parse data with GetField, but it is uncomfortable. Maybe there are more ways to solve the problem?
Beta Was this translation helpful? Give feedback.
All reactions