Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert DataTable to List<T> using Reflections

I have a Generic list of a class that I automatically convert it to DataTable using Reflection and extension methods.Now I want to do it in reverse direction.I want to convert DataTable to List.Better to say I want help to write a method that expect a DataTable and a Type and automatically find property of that type(class) according to column name and assign value to object of that Type(class).like this psodu code:

private List<T> ConvertToList<T>(DataTable dt)
{
    List<string> AllColumns = // Get All Column Names of a DataTable
    for(int i=0;i<dt.Rows.Count;i++)
    {
        foreach(var item in AllColumns )
        {
             //Get Property According To **ITEM**
             //Get Data Of Rows[i][item] and assign it to T.property
        }
    }
}

How I can do this?


Edit 1)

I use answer of @Cuong Le like this:

var properties = typeof(CustomType).GetProperties().ToList();
List<CustomType> list = ConvertToList<CustomType>(dt, properties);

and :

private List<T> ConvertToList<T>(DataTable dt,List<PropertyInfo> fields) where T : class
{
    return dt.AsEnumerable().Select(Convert<T>(fields)).ToList();  <------
}

private T Convert<T>(DataRow row,List<PropertyInfo> fields) where T : class
{
    var properties = typeof(T).GetProperties().ToList();

    var objT = Activator.CreateInstance<T>();
    foreach (var pro in properties)
    {
        pro.SetValue(objT, row[pro.Name],null);
    }

    return objT;
} 

but in line I place an arrow in front of it I got this two errors:

No overload for method 'Convert' takes 1 arguments

and

The type arguments for method 'System.Data.EnumerableRowCollectionExtensions.Select(System.Data.EnumerableRowCollection, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

How I can solve this problem?

like image 594
Arian Avatar asked Sep 30 '12 15:09

Arian


1 Answers

Use AsEnumerable() method to support LINQ:

    private List<T> ConvertToList<T>(DataTable dt)
    {
        var columnNames = dt.Columns.Cast<DataColumn>()
            .Select(c => c.ColumnName)
            .ToList();

        var properties = typeof(T).GetProperties();

        return dt.AsEnumerable().Select(row =>
            {
                var objT = Activator.CreateInstance<T>();

                foreach (var pro in properties)
                {
                    if (columnNames.Contains(pro.Name))
                        pro.SetValue(objT, row[pro.Name]);
                }

                return objT;
            }).ToList();

    }

GetProperties searches for the properties of the current Type, using the specified binding constraints.

The link in here: http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx

like image 145
cuongle Avatar answered Oct 15 '22 15:10

cuongle