Fayakun.Extensions.DataTable
1.1.0
dotnet add package Fayakun.Extensions.DataTable --version 1.1.0
NuGet\Install-Package Fayakun.Extensions.DataTable -Version 1.1.0
<PackageReference Include="Fayakun.Extensions.DataTable" Version="1.1.0" />
<PackageVersion Include="Fayakun.Extensions.DataTable" Version="1.1.0" />
<PackageReference Include="Fayakun.Extensions.DataTable" />
paket add Fayakun.Extensions.DataTable --version 1.1.0
#r "nuget: Fayakun.Extensions.DataTable, 1.1.0"
#:package Fayakun.Extensions.DataTable@1.1.0
#addin nuget:?package=Fayakun.Extensions.DataTable&version=1.1.0
#tool nuget:?package=Fayakun.Extensions.DataTable&version=1.1.0
Fayakun.Extensions.DataTable
Fast, zero-dependency conversion between DataTable / DataRow and your own classes, in both directions. Supports .NET Framework 3.5 through .NET 10.
🚀 Features
- Rows to models:
ToArray<T>(),ToList<T>(), a lazyAsEnumerable<T>()andDataRow.ToModel<T>(). - Models to a table:
IEnumerable<T>.ToDataTable(), ready forSqlBulkCopy, table-valued parameters and reports. - Forgiving matching: Column names match property names case-insensitively (an exact-case match wins). Extra columns and read-only properties are ignored.
- Automatic type conversion: Numeric widening and narrowing,
Nullable<T>, enums (from names or numbers),Guid,DateTimeOffset,TimeSpanandboolfrom0/1. - Fast: Property maps are cached per type, columns are resolved once per table, and setters are compiled.
- Clear errors: A failed conversion throws
InvalidCastExceptionnaming the column, the value and the property.
📦 Installation
dotnet add package Fayakun.Extensions.DataTable
💻 Usage Examples
using Fayakun.Extensions.DataTable;
public class Customer
{
public int Id { get; set; }
public string? Name { get; set; }
public decimal? Balance { get; set; }
public CustomerStatus Status { get; set; }
}
DataTable to Models
DataTable table = LoadFromDatabase(); // columns: ID, NAME, Balance, Status
Customer[] array = table.ToArray<Customer>();
List<Customer> list = table.ToList<Customer>();
foreach (var customer in table.AsEnumerable<Customer>()) // maps rows one at a time
{
// ...
}
Customer? first = table.Rows[0].ToModel<Customer>();
DBNull becomes null, or the default value for non-nullable value types.
Models to a DataTable
DataTable table = customers.ToDataTable("Customers");
using var bulk = new SqlBulkCopy(connection) { DestinationTableName = "dbo.Customers" };
await bulk.WriteToServerAsync(table);
Each public readable property becomes a column (a Nullable<T> property becomes a nullable column of type T). null items are skipped.
🔐 Notes
- Model types need a public parameterless constructor. Classes and structs are both supported.
- Mapping uses reflection, so the package is not marked trim/AOT-safe.
- On .NET Framework 3.5, setters use cached reflection, because expression-tree assignment is unavailable there.
🎯 Target Frameworks
| Target | Notes |
|---|---|
| .NET Framework 3.5, 4.0, 4.5 | Legacy applications |
| .NET Standard 2.0 | .NET Framework 4.6.2+, Mono, Unity |
| .NET Core 3.1, .NET 6, .NET 8, .NET 10 | LTS runtimes |
📚 API Reference
| Method | Description |
|---|---|
DataTable.ToArray<T>() |
Maps all rows to an array. |
DataTable.ToList<T>() |
Maps all rows to a list. |
DataTable.AsEnumerable<T>() |
Maps rows lazily as you enumerate. |
DataRow.ToModel<T>() |
Maps one row. |
IEnumerable<T>.ToDataTable(string? tableName = null) |
Creates a typed table from a collection. |
📄 License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 is compatible. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net35 is compatible. net40 is compatible. net403 was computed. net45 is compatible. net451 was computed. net452 was computed. net46 was computed. 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. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 3.5
- No dependencies.
-
.NETFramework 4.0
- No dependencies.
-
.NETFramework 4.5
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
net10.0
- No dependencies.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v1.1.0
- New: IEnumerable<T>.ToDataTable() creates a typed DataTable from any collection.
- New targets: netcoreapp3.1, net6.0, net8.0 and net10.0.
- Much faster: property maps are built once per type, columns are resolved once per table, and setters are compiled (.NET Framework 4.0+).
- Column names are matched case-insensitively, and an exact-case match wins.
- Values are converted to the property type: numeric widening and narrowing, Nullable<T>, enums (from names or numbers), Guid, DateTimeOffset, TimeSpan and bool.
- AsEnumerable<T>() is now lazy and maps rows as they are enumerated.
- Read-only and indexer properties are skipped instead of throwing.
- Conversion failures throw InvalidCastException naming the column and property.