diff options
Diffstat (limited to 'ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/configuration/class-maps/mapping-by-index/index.md')
-rw-r--r-- | ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/configuration/class-maps/mapping-by-index/index.md | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/configuration/class-maps/mapping-by-index/index.md b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/configuration/class-maps/mapping-by-index/index.md new file mode 100644 index 0000000..b0b96a8 --- /dev/null +++ b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/configuration/class-maps/mapping-by-index/index.md @@ -0,0 +1,41 @@ +# Mapping by Index + +If your data doesn't have a header you can map by index instead of name. You can't rely on the order of class properties in .NET, so if you're not mapping by name, make sure you specify an index. + +###### Data +``` +1,one +``` + +###### Example + +```cs +void Main() +{ + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + }; + using (var reader = new StreamReader("path\\to\\file.csv")) + using (var csv = new CsvReader(reader, config)) + { + csv.Context.RegisterClassMap<FooMap>(); + var records = csv.GetRecords<Foo>(); + } +} + +public class Foo +{ + public int Id { get; set; } + public string Name { get set; } +} + +public sealed class FooMap : ClassMap<Foo> +{ + public FooMap() + { + Map(m => m.Id).Index(0); + Map(m => m.Name).Index(1); + } +} +``` |