Created
December 18, 2013 20:37
-
-
Save randyburden/8029470 to your computer and use it in GitHub Desktop.
Demonstrates converting a DataTable to a list of strongly typed objects using Slapper.AutoMapper.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Linq; | |
using System.Reflection; | |
using NUnit.Framework; | |
namespace Atcs.Core.Tests | |
{ | |
[TestFixture] | |
class DataTableMappingTestsWithSlapperAutoMapper | |
{ | |
/// <summary> | |
/// Demonstrates converting a DataTable to a list of strongly typed objects using Slapper.AutoMapper | |
/// </summary> | |
[Test] | |
public void ConvertDataTableToStronglyTypedObjectsViaSlapperAutoMapper() | |
{ | |
// Create a list of Persons | |
var listOfSuperheroesRealIdenties = new List<Person> | |
{ | |
new Person { Id = 1, FirstName = "Clark", LastName = "Kent" }, | |
new Person { Id = 2, FirstName = "Bruce", LastName = "Wayne" }, | |
new Person { Id = 3, FirstName = "Peter", LastName = "Parker" } | |
}; | |
DataSet dataSet = new DataSet(); | |
// Convert the list of Person objects to a data table | |
DataTable dataTable = CreateDataTable( listOfSuperheroesRealIdenties ); | |
dataSet.Tables.Add( dataTable ); | |
DataTable table = dataSet.Tables[ 0 ]; | |
DataTableReader reader = table.CreateDataReader(); | |
List<Dictionary<string, object>> dictionaries = new List<Dictionary<string, object>>(); | |
// Convert the DataTable to a list of dictionaries | |
if ( reader.HasRows ) | |
{ | |
while ( reader.Read() ) | |
{ | |
Dictionary<string, object> dictionary = Enumerable.Range( 0, reader.FieldCount ) | |
.ToDictionary( i => reader.GetName( i ), i => reader.GetValue( i ) ); | |
dictionaries.Add( dictionary ); | |
} | |
} | |
// Convert the list of dictionaries to a list of Persons | |
// Get Slapper.AutoMapper from NuGet: http://www.nuget.org/packages/Slapper.AutoMapper/ | |
// Get Slapper.AutoMapper from GitHub: https://github.com/randyburden/Slapper.AutoMapper | |
List<Person> persons = Slapper.AutoMapper.Map<Person>( dictionaries ).ToList(); | |
// Verify that there are the same number of persons in each list | |
Assert.That( persons.Count == listOfSuperheroesRealIdenties.Count ); | |
} | |
public class Person | |
{ | |
public int Id { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} | |
/// <summary> | |
/// Creates a <see cref="System.Data.DataTable"/> from a list of <typeparamref name="T"/>. | |
/// </summary> | |
/// <typeparam name="T">Object type.</typeparam> | |
/// <param name="list">List of <typeparamref name="T"/>.</param> | |
/// <returns>DataTable representation of <typeparamref name="T"/>'s property names and values.</returns> | |
public static DataTable CreateDataTable<T>( List<T> list ) where T : class | |
{ | |
Type type = typeof( T ); | |
DataTable dataTable = new DataTable( type.Name ); | |
foreach ( PropertyInfo pi in type.GetProperties() ) | |
{ | |
dataTable.Columns.Add( new DataColumn( pi.Name ) ); | |
} | |
if ( list != null ) | |
{ | |
foreach ( T item in list ) | |
{ | |
DataRow dataRow = dataTable.NewRow(); | |
foreach ( DataColumn dc in dataTable.Columns ) | |
{ | |
dataRow[ dc.ColumnName ] = item.GetType().GetProperty( dc.ColumnName ).GetValue( item, null ); | |
} | |
dataTable.Rows.Add( dataRow ); | |
} | |
} | |
return dataTable; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment