How to convert List to datatable
-
Hi I have one Generic List how to convert the list into data table?. Any one help me please?. Regards Kesavan
kesavan
-
AFAIK there is only the long way - loop through the list and add each item
Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP
I don't really know why you need it. In many cases is better to loop throght the items and execute the query directly. But: You can generate a common code that: Uses Reflection to discover the properties/fields of the classes. Be able to map the fields to the columns with the same name or, to create such columns in the datatable (that depends on your need). Then, loop at each item adding a row and loop for each field/property setting the right column value.
-
Hi I have one Generic List how to convert the list into data table?. Any one help me please?. Regards Kesavan
kesavan
CSLA has/had a class called ObjectAdaptor that was designed to do what you want.
-
Hi I have one Generic List how to convert the list into data table?. Any one help me please?. Regards Kesavan
kesavan
What type of list? For list of int you can do it like this:
List<int> myList = new List<int>(100); for (int i = 1; 1 <= 50; i++) { myList.Add(i); } // Create DataTable without column and row DataTable dt = new DataTable("MyTbl"); // Add an integer Column dt.Columns.Add("MyColumn", Type.GetType("System.Int32")); for (int j = 0; j < myList.Count; j++) // fill DataTable { dt.Rows.Add(myList\[j\]); }