C# - How do you convert a DataTable into a List

Using the following method you can convert the DataTable into a List

For example you have the following DataTable:

DataTable resultTable = ConvertToDataTableFromList(realTimeSalesInput.RealTimeSales);
DataTable salesDt = new DataTable("RealTimeSales");
salesDt.Columns.Add("StoreNumber", typeof(string));
salesDt.Columns.Add("Value", typeof(decimal));
    //Test Data
salesDt.Rows.Add(100, "A100", 100);
salesDt.Rows.Add(101, "B100", 101);
salesDt.Rows.Add(102, "C100", 102);

Now you can call the following function to provide the List from the given DataTable.

    /// <summary>
    /// RealTimeSales
    /// </summary>
    public class RealTimeSales
    {
        public long StoreID { get; set; }

        public string StoreNumber { get; set; }

        public decimal Value { get; set; }
    }

    List<RealTimeSales> salesList = new List<RealTimeSales>();
    salesList = ConvertToListFromDataTable<RealTimeSales>(salesDt);

        /// <summary>
        /// Converts to list from data table.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt">The dt.</param>
        /// <returns></returns>
        public List<T> ConvertToListFromDataTable<T>(System.Data.DataTable dt)
        {
            System.Collections.Generic.List<T> data = new List<T>();
            foreach (System.Data.DataRow row in dt.Rows)
            {
                T item = GetItemValue<T>(row);
                data.Add(item);
            }
            return data;
        }

        /// <summary>
        /// Gets the item.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dr">The dr.</param>
        /// <returns></returns>
        public T GetItemValue<T>(System.Data.DataRow dr)
        {
            System.Type temp = typeof(T);
            T obj = Activator.CreateInstance<T>();

            foreach (DataColumn column in dr.Table.Columns)
            {
                foreach (System.Reflection.PropertyInfo pro in temp.GetProperties())
                {
                    if (pro.Name == column.ColumnName)
                        pro.SetValue(obj, dr[column.ColumnName], null);
                    else
                        continue;
                }
            }
            return obj;
        }


What is GST

GST (Goods and Service Tax) is a comprehensive tax on the supply of goods and services at each stage of any transaction. Read More

Income Tax Information

An income tax is a tax imposed by government on income earned by you. Income tax is a key source of funds that the government uses to fund its activities and serve the public. Read More

General Insurance Companies in India

General insurance is insurance for valuables other than our life and health. General insurance covers the insurer against damage, loss and theft of your valuables. Read More



Types of Bank Loans In India

Loan means lending money from one individual or entity to another. A loan has three components – principal or the borrowed amount, rate of interest and tenure or duration for which the loan is availed. Read More

List of Banks in India

The Reserve Bank of India is the central Bank that is fully owned by the Government of India. It is governed by a central board (headed by a Governor) appointed by the Central Government. Read More

List of Educational Institutions in India

The following list of comprehensive websites on higher education in India. These websites will provide detailed information on education system in India. Read More