Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions files-csharp/ReadFromCsv/ReadFromCsv/Person.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using CsvHelper.Configuration.Attributes;
using CsvHelper;

namespace ReadFromCsv;

public class Person
[CsvRecord]
public partial class Person
{
[Index(0)]
[CsvColumn(Index = 0)]
public int Id { get; set; }
[Index(1)]
[CsvColumn(Index = 1)]
public string? Name { get; set; }
[Index(2)]
[CsvColumn(Index = 2)]
public bool IsLiving { get; set; }
[Index(3)]
[CsvColumn(Index = 3)]
public DateTime DateOfBirth { get; set; }
}
30 changes: 21 additions & 9 deletions files-csharp/ReadFromCsv/ReadFromCsv/PersonMap.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using CsvHelper.Configuration;
using CsvHelper;

namespace ReadFromCsv;
namespace ReadFromCsv.Mapped;

public class PersonMap : ClassMap<Person>
// A class we cannot annotate, such as one from another library.
public class Person
{
public PersonMap()
{
Map(p => p.Id).Index(0);
Map(p => p.Name).Index(1);
Map(p => p.IsLiving).Index(2);
}
public int Id { get; set; }
public string? Name { get; set; }
public bool IsLiving { get; set; }
public DateTime DateOfBirth { get; set; }
}

[CsvRecordMap(typeof(Person))]
public partial class PersonMap
{
[CsvColumn(Index = 0)]
public int Id { get; set; }
[CsvColumn(Index = 1)]
public string? Name { get; set; }
[CsvColumn(Index = 2)]
public bool IsLiving { get; set; }
[CsvColumn(Index = 3)]
public DateTime DateOfBirth { get; set; }
}
4 changes: 2 additions & 2 deletions files-csharp/ReadFromCsv/ReadFromCsv/ReadFromCsv.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="33.1.0" />
<PackageReference Include="CsvHelper" Version="34.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 5 additions & 10 deletions files-csharp/ReadFromCsv/ReadFromCsv/ReadMethods.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
using CsvHelper;
using CsvHelper.Configuration;
using System.Globalization;

namespace ReadFromCsv;

public static class ReadMethods
{
public static List<Person> ReadPersons()
{
var configuration = new CsvConfiguration(CultureInfo.InvariantCulture)
var options = new CsvReaderOptions
{
Delimiter = ",",
Comment = '#',
HasHeaderRecord = false
Separator = ',',
CommentPrefix = '#',
HasHeader = false
};

using var reader = new StreamReader("filePersons.csv");
using var csv = new CsvReader(reader, configuration);

csv.Context.RegisterClassMap<PersonMap>();
using var csv = CsvReader.FromFile("filePersons.csv", options);

return csv.GetRecords<Person>().ToList();
}
Expand Down
Loading
Loading