DataTableMapper 1.4.0

dotnet add package DataTableMapper --version 1.4.0
                    
NuGet\Install-Package DataTableMapper -Version 1.4.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DataTableMapper" Version="1.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DataTableMapper" Version="1.4.0" />
                    
Directory.Packages.props
<PackageReference Include="DataTableMapper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DataTableMapper --version 1.4.0
                    
#r "nuget: DataTableMapper, 1.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DataTableMapper@1.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DataTableMapper&version=1.4.0
                    
Install as a Cake Addin
#tool nuget:?package=DataTableMapper&version=1.4.0
                    
Install as a Cake Tool

DataTableMapper

CI NuGet

Map a DataTable to a class instance via the MapTo<T>() extension method. The table's columns automatically map to the class' properties by name. Attributes are available for mapping to a column with a different name, for default values, and for converting one value to another before setting the property.

Targets netstandard2.0 (.NET Framework 4.6.1+, .NET Core 2.0+) and net10.0.

Install

dotnet add package DataTableMapper

Usage

The extension method signature is:

public static IEnumerable<T> MapTo<T>(this System.Data.DataTable table) where T : new()

Call it:

IEnumerable<MyClass> x = table.MapTo<MyClass>();

Mapping

MapTo attempts to find a value for each property of the class in the following order:

  1. ColumnMappingAttribute — map the property to a column with another name (not case sensitive)
  2. Property name — search for a column with the property's name (not case sensitive)
  3. DefaultValueAttribute — set a default value when steps 1 and 2 could not produce one

Column mapping

By default MapTo maps a property to the table's column with the same name.

Decorate a property with ColumnMappingAttribute to map it to a column with another name. MapTo still falls back to property-name mapping if no match is found. For example, MapTo will look for a column named Id for the property below:

public class MyClass
{
    [ColumnMapping("Id")]
    public int MyClassId { get; set; }
}

ColumnMappingAttribute can be inherited for custom functionality.

Default values

Decorate a property with DefaultValueAttribute to assign a value when no mapping can be done, or when the mapping yields a DBNull:

class Person
{
    [DefaultValue(99)]
    public int Id { get; set; }

    [DefaultValue("Johnny")]
    public string Name { get; set; }

    [DefaultValue(true)]
    public bool IsGreat { get; set; }
}

Conversion

For columns returning a different type to the property type, BoolValueConversionAttribute (shipped with the library) converts a column returning an integer to a boolean with C-like rules — zero is false, any non-zero value is true:

class MyClass
{
    // Looks for a column named "val"; if the row value is non-zero the property is set to true, else false
    [BoolValueConversion]
    public bool Val { get; set; }
}

Extensibility

For a custom conversion (e.g. decryption), create an attribute implementing the IValueConversion interface.

Also inherit ColumnMappingAttribute to get column mapping with the attribute.

Building

Requires the .NET SDK 10 (see global.json).

dotnet build -c Release
dotnet test  -c Release
dotnet pack  DataTableMapper/DataTableMapper.csproj -c Release -o artifacts

Releasing

  1. Bump <VersionPrefix> in Directory.Build.props.
  2. Commit, then tag: git tag v1.2.0 && git push --follow-tags.
  3. The Release workflow verifies the tag matches the project version, packs, and pushes to NuGet.org via Trusted Publishing (OIDC, no stored API key). It needs a nuget-release GitHub environment and a matching Trusted Publishing policy on nuget.org (see .github/workflows/release.yml).

Licence

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.0 93 9/5/2026
1.3.0 92 9/5/2026
1.2.0 106 9/3/2026
1.1.1 25,350 12/11/2015
1.1.0 2,914 11/21/2015
1.0.7 2,410 9/13/2015
1.0.6 2,793 8/6/2015
1.0.5 2,338 8/5/2015
1.0.4 2,158 7/29/2015
1.0.4-pre 2,045 7/29/2015
1.0.3 3,697 4/19/2015
1.0.2-beta 3,137 4/18/2015
0.1.0 2,894 4/11/2015

1.4.0 - byte[] properties now map (were silently left null); this is the type ADO.NET uses for varbinary and rowversion/timestamp columns. 1.3.0 - Type-conversion correctness fixes: Guid/TimeSpan/DateTimeOffset properties now map (were silently left empty); nullable enums map from strings; string-to-number/date parsing is now invariant-culture; [DefaultValue] applies alongside [BoolValueConversion]; cyclic object graphs no longer overflow the stack; BoolValueConversion treats any non-zero number as true.