If you want to convert a generic list to DataTable you can use the below extension method for IList<T>
Let's say you have the following list with you
List<someClass> someClassObjectList = new List<someClass>();
DataTable someDataTable = new DataTable();
and you want to convert the someClassObjectList to DataTable object
The possible solution you end up by writing a convert or translate method which will convert the each and every property and assigns to DataTable object, something looks like this
By using the above code also your job will get done, but what if your class has 100 properties and what if you want convert lots of different generic lists like this. You can't write the different method for the different generic list, and you don't have the patience to list the 100 properties like above.
Now what do, you can write a single extension method.
Create a static class in your project with some name like "Extensions" and write the below code in that class
Now in your actual class wherever you want to convert the generic list to DataTable write code like below
var someDataTable = someClassObjectList .ToDataTable<someClass>();
using a single method you can convert any generic list to DataTable.
var anotherDataTable = anotherClassObjectList .ToDataTable<anotherClass>();
var oneMoreDataTable = oneMoreClassObjectList .ToDataTable<oneMoreClass>();
.
.
.
Write once use anywhere.
Let's say you have the following list with you
List<someClass> someClassObjectList = new List<someClass>();
DataTable someDataTable = new DataTable();
and you want to convert the someClassObjectList to DataTable object
The possible solution you end up by writing a convert or translate method which will convert the each and every property and assigns to DataTable object, something looks like this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public DataTable ConvertsomeClassTosomeDataTable(List<someClass> someClassObjectList) | |
{ | |
DataTable convertedsomeClassObjectList = new DataTable("SomeDataTable"); | |
// Lest assume we have 3 properties in your someClass Class | |
convertedsomeClassObjectList.Columns.Add("Property1", typeof(int)); | |
convertedsomeClassObjectList.Columns.Add("Property2", typeof(DateTime)); | |
convertedsomeClassObjectList.Columns.Add("Property3", typeof(int)); | |
foreach (someClass obj in someClassObjectList) | |
{ | |
convertedsomeClassObjectList.Rows.Add( | |
obj.Property1, | |
obj.Property2, | |
obj.Property3); | |
} | |
return convertedsomeClassObjectList; | |
} |
By using the above code also your job will get done, but what if your class has 100 properties and what if you want convert lots of different generic lists like this. You can't write the different method for the different generic list, and you don't have the patience to list the 100 properties like above.
Now what do, you can write a single extension method.
Create a static class in your project with some name like "Extensions" and write the below code in that class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Extensions | |
{ | |
public static DataTable ToDataTable<T>(this IList<T> data) | |
{ | |
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); | |
DataTable DataTable = new DataTable(); | |
for (int i = 0; i < properties.Count; i++) | |
{ | |
PropertyDescriptor prop = properties[i]; | |
DataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType( | |
prop.PropertyType) ?? prop.PropertyType); | |
} | |
object[] objectValues = new object[properties.Count]; | |
foreach (T item in data) | |
{ | |
for (int i = 0; i < objectValues.Length; i++) | |
{ | |
objectValues[i] = properties[i].GetValue(item); | |
} | |
DataTable.Rows.Add(objectValues); | |
} | |
DataTable.TableName = typeof(T).Name + "Your Table Name"; | |
return DataTable; | |
} | |
} |
var someDataTable = someClassObjectList .ToDataTable<someClass>();
using a single method you can convert any generic list to DataTable.
var anotherDataTable = anotherClassObjectList .ToDataTable<anotherClass>();
var oneMoreDataTable = oneMoreClassObjectList .ToDataTable<oneMoreClass>();
.
.
.
Write once use anywhere.