Code review for converting datatable to list RRS feed

  • Question

  • see the code

            public static List<T> ConvertToDataTable<T>(DataTable dt)
                List<T> data = new List<T>();
                foreach (DataRow row in dt.Rows)
                    T item = GetItem<T>(row);
                return data;
            private static T GetItem<T>(DataRow dr)
                Type temp = typeof(T);
                T obj = Activator.CreateInstance<T>();
                foreach (DataColumn column in dr.Table.Columns)
                    foreach (PropertyInfo pro in temp.GetProperties())
                        if (pro.Name == column.ColumnName)
                            pro.SetValue(obj, dr[column.ColumnName], null);
                return obj;

    calling this way

                var JoinResult2 = (from p in dt.AsEnumerable().AsParallel() /*.AsOrdered()*/
                                   join t in dtTax.AsEnumerable().AsParallel() /*.AsOrdered()*/
                                   on p.Field<int>("Tax Id") equals t.Field<int>("Tax Id") into pj
                                   from r in pj.DefaultIfEmpty()
                                   select new
                                       ProductName = p.Field<string>("Product Name"),
                                       BrandName = p.Field<string>("Brand Name"),
                                       ProductCategory = r == null ? "" : r.Field<string>("Product Category"),
                                       TaxCharge = r == null ? 0 : r.Field<int>("Charge")
                List<MyData> mc = new List<MyData>();
                mc = Extension.ConvertToDataTable<MyData>(JoinResult2);  
                dataGridView1.DataSource = mc;

    tell me when there will be huge data in datatable then does the above function ConvertToDataTable() will work faster?

    if not then what will be best approach ?

    how to map if my property name & datatable column name would be different ?

    Sunday, August 25, 2019 7:03 PM

All replies

  • Hello,

    A DataTable initialized as per the converter is always going to take time so why not use a List<T> and if for say a Windows Form project combine this with a BindingList.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Sunday, August 25, 2019 7:32 PM
  • The first possible improvement is to put a break inside the if block.

    The second possible improvement is to remove ToList if ToDataTable does not need it.

    The third possible improvement is to get all of the target properties of T in advance, avoiding repetitive operations.

    The fourth possible improvement is to rename ConvertToDataTable to ConvertToList, for example.

    The final possible improvement is to try something like this:

    List<MyData> mc = (from p in dt.AsEnumerable()

                                   join t in dtTax.AsEnumerable()

                                   on p["Tax Id"] equals t["Tax Id"] into pj

                                   from r in pj.DefaultIfEmpty()

                                   select new MyData


                                       ProductName = p.Field<string>("Product Name"),

                                       BrandName = p.Field<string>("Brand Name"),

                                       ProductCategory = r == null ? "" : r.Field<string>("Product Category"),

                                       TaxCharge = r == null ? 0 : r.Field<int>("Charge")



    Monday, August 26, 2019 5:49 AM
  • one request. can you please post the updated code of my function ConvertToDataTable ?

    you discuss few point to change which are not clear. so can post your version of code which will be performant. thanks

    Monday, August 26, 2019 3:03 PM
  • Hi Sudip_inn,

    If you want to deal with huge data in datatable, I don't suggest that you use linq in a datatable. I suggest that you could store the data into database. You could use DataReader to do it.

    The following link has a sentence said that 'The DataReader is a good choice when you're retrieving large amounts of data because the data is not cached in memory'.

    Retrieve data using a DataReader

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, August 29, 2019 6:55 AM