Home > DeveloperSection > Forums > How to convert List<dynamic> to List<OurClass>
Ashish Pandey

Total Post:128

Posted on    November-27-2014 12:40 AM

 C# C# 

 1 Reply(s)
 1443  View(s)
Rate this:

I create a dynamic list at run-time and after execution I need to assign the list to my class properties


List<tblchargemaster> charge = new List<tblchargemaster>();


charge = (List<tblchargemaster>) ObjDB.SelectQuery("tblchargemaster");

tblchargemaster is the class type and SelectQuery function returns List<dynamic>. How can I convert this dynamic list to a list with my class type?


     public List<dynamic> SelectQuert(string TableName)




        DataTable dt = new DataTable("map");


        string Query = "Select * from " + TableName;           

        MySqlDataAdapter dr = new MySqlDataAdapter(Query, Conn);



        List<dynamic> dynamicDt = dt.ToDynamic();




        return dynamicDt;


And Generic Class is


    public static class DataTableExtensions


    public static List<dynamic> ToDynamic(this DataTable dt)


        var dynamicDt = new List<dynamic>();

        foreach (DataRow row in dt.Rows)


            dynamic dyn = new ExpandoObject();

            foreach (DataColumn column in dt.Columns)


                var dic = (IDictionary<string, object>)dyn;

                dic[column.ColumnName] = row[column];




        return dynamicDt;



Pravesh Singh

Total Post:412

Posted on    November-27-2014 1:52 AM

Hi Ashish, 

Use Cast<tblchargemaster>() method:

List<tblchargemaster> charge = new List<tblchargemaster>();

charge = charge.AddRange(


Don't want to miss updates? Please click the below button!

Follow MindStick