ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

DataTableToList

Convert DataTable To List

Source

public static List<T> DataTableToList<T>(DataTable table) where T 
	: class, new()
{
    try
    {
        T tempT = new T();
        var tType = tempT.GetType();
        List<T> list = new List<T>();
        foreach (var row in table.Rows.Cast<DataRow>())
        {
            T obj = new T();
            foreach (var prop in obj.GetType().GetProperties())
            {
                var propertyInfo = tType.GetProperty(prop.Name);
                var rowValue = row[prop.Name];
                var t = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
                object safeValue = (rowValue == null || DBNull.Value.Equals(rowValue)) ? null : Convert.ChangeType(rowValue, t);
                propertyInfo.SetValue(obj, safeValue, null);
            }
            list.Add(obj);
        }
        return list;
    }
    catch
    {
        return null;
    }
}

Example

using System;
using System.Data;
using System.Data.DataSetExtensions;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		DataTable dt = new DataTable();
		dt.Columns.Add("id");
		dt.Columns.Add("name");
		dt.Columns.Add("foo");
		dt.Columns.Add("foo2");
		dt.Columns.Add("foo3");
		dt.Columns.Add("foo4");
		for(int i=0;i<=1000;i++){dt.Rows.Add(i, "foo", 1.1, null, true, DateTime.Now);}
		
		var typedList = DataTableToList<Class1>(dt);
		
		Console.WriteLine("Spotchecking first value");
		Console.WriteLine(typedList[0].foo);
		Console.WriteLine(typedList[0].foo2);
		Console.WriteLine(typedList[0].foo3);
		Console.WriteLine(typedList[0].foo4);
	}
	
public static List<T> DataTableToList<T>(DataTable table) where T 
	: class, new()
{
    try
    {
        T tempT = new T();
        var tType = tempT.GetType();
        List<T> list = new List<T>();
        foreach (var row in table.Rows.Cast<DataRow>())
        {
            T obj = new T();
            foreach (var prop in obj.GetType().GetProperties())
            {
                var propertyInfo = tType.GetProperty(prop.Name);
                var rowValue = row[prop.Name];
                var t = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
                object safeValue = (rowValue == null || DBNull.Value.Equals(rowValue)) ? null : Convert.ChangeType(rowValue, t);
                propertyInfo.SetValue(obj, safeValue, null);
            }
            list.Add(obj);
        }
        return list;
    }
    catch
    {
        return null;
    }
}


}

public class Class1{
	public int id {get;set;}
	public string name {get;set;}
	public Double foo {get;set;}
	public int? foo2 {get;set;}
	public bool foo3 {get;set;}
	public DateTime foo4 {get;set;}
}

Author: Ravi Kumhar

Submitted on: 29 mei 2019

Language: C#

Type: DataTableToList

Views: 5766