A T[] extension method that converts the @this to a DataTable. - CSharp System.Data

CSharp examples for System.Data:DataTable

Description

A T[] extension method that converts the @this to a DataTable.

Demo Code

// Copyright (c) 2015 ZZZ Projects. All rights reserved
using System.Reflection;
using System.Data;
using System;/* ww w .  j a  v a 2s. co m*/

public class Main{
        /// <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;
    }
}

Related Tutorials