Array - ToDataTable
A T[] extension method that converts the @this to a data table.
public static void Main() { var myObjs = new[] { new TestObject { IntColumn = 1, StringColumn = "2" }, new TestObject { IntColumn = 3, StringColumn = "4" } }; // C# Extension Method: Array - ToDataTable DataTable dt = myObjs.ToDataTable(); // Unit Test Console.WriteLine(dt.Rows.Count); Console.WriteLine(dt.Rows[0]["IntColumn"]); Console.WriteLine(dt.Rows[0]["StringColumn"]); Console.WriteLine(dt.Rows[1]["IntColumn"]); Console.WriteLine(dt.Rows[1]["StringColumn"]); } public class TestObject { public int IntColumn; public string StringColumn { get; set; } }
View Source
using System; using System.Data; using System.Reflection; public static partial class Extensions { /// <summary> /// A T[] extension method that converts the @this to a data table. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <returns>@this as a DataTable.</returns> public static DataTable ToDataTable<T>(this T[] @this) { Type type = typeof (T); PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); var dt = new DataTable(); foreach (PropertyInfo property in properties) { dt.Columns.Add(property.Name, property.PropertyType); } foreach (FieldInfo field in fields) { dt.Columns.Add(field.Name, field.FieldType); } foreach (T item in @this) { DataRow dr = dt.NewRow(); foreach (PropertyInfo property in properties) { dr[property.Name] = property.GetValue(item, null); } foreach (FieldInfo field in fields) { dr[field.Name] = field.GetValue(item); } dt.Rows.Add(dr); } return dt; } }