wagnerhsu / sample-ToDataTable

Highly performant .NET Core library to convert collections of objects into DataTables

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ToDataTable

What is this?

ToDataTable is a pair of .Net Core extension methods that allow you to create a .Net DataTable or a SQL Server Table-Valued Parameter from a collection of objects, e.g:

IEnumerable<SomeClass> enumerable = new List<SomeClass();
DataTable dataTable = enumerable.ToDataTable();
SqlParameter sqlParameter = enumerable.ToSqlParameter("@SqlParameterName","SqlUserDefinedDataTypeName")

How do I get it?

Install ToDataTable via nuget Package Manager:

Install-Package ToDataTable -Version 0.1.2

or .Net CLI:

dotnet add package ToDataTable --version 0.1.2

Give me an example:

Say you had a Horse class:

public class Horse
{
    public string Name { get; set;}
    public string Occupation { get; set;}
    public int Legs { get; set; }
    public double TopSpeed {get; set; }
}

and you created an array of 2 Horses :

var horseArray = new[] {
    new Horse {
	    Name = "Dobin",
	    Occupation "Code Horse",
	    Legs = 4,
	    TopSpeed = 18.6
    },
    new Horse {
	    Name = "Plato",
	    Occupation "Curious Gelding",
	    Legs = 4,
	    TopSpeed = 22.3
    }
}

You'd use the ToDataTable() extension method to create a DataTable from your array:

DataTable myDataTable = horseArray.ToDataTable();

The resulting table would look like this:

Name Occupation Legs TopSpeed
Dobin Code Horse 4 18.6
Plato Curious Gelding 4 22.3

Why is this useful?

Sql Table-valued parameters are created in .Net using DataTables. Coding a DataTable from scratch is time consuming and prone to error. These extension methods can create a Datatable or a SqlParameter from a collection of objects in a single line of code.

Is it fast?

You bet. The code uses reflection to build methods for creating DataTables and adding DataRows. The reflection code is only run once per type and cached to a singleton context object. Caching makes the code run significantly faster than using reflection alone.

I used BenchmarkDotNet to measure the cached methods vs just reflecton. The Benchmark tests are included in the repo.

About

Highly performant .NET Core library to convert collections of objects into DataTables

License:MIT License


Languages

Language:C# 100.0%