How do you convert a DataTable into a generic list?
Asked Answered
G

29

206

Currently, I'm using:

DataTable dt = CreateDataTableInSomeWay();

List<DataRow> list = new List<DataRow>(); 
foreach (DataRow dr in dt.Rows)
{
    list.Add(dr);
}

Is there a better/magic way?

Graupel answered 16/10, 2008 at 13:23 Comment(2)
What are you trying to accomplish with a List that you can't do with your DataRowCollection?Pavla
Mine is late but hope will be useful. Working solution..https://mcmap.net/q/20522/-fastest-way-to-convert-datatable-to-generic-listHeadpin
S
304

If you're using .NET 3.5, you can use DataTableExtensions.AsEnumerable (an extension method) and then if you really need a List<DataRow> instead of just IEnumerable<DataRow> you can call Enumerable.ToList:

IEnumerable<DataRow> sequence = dt.AsEnumerable();

or

using System.Linq;
...
List<DataRow> list = dt.AsEnumerable().ToList();
Schaper answered 16/10, 2008 at 13:59 Comment(12)
how to convert this list to json.Gopher
@Pandiya: There are various ways of converting data into JSON in .NET. Personally I've always used the JSON.NET library, but there are other approaches too.Schaper
stackoverflow.com/questions/3482261/…Gopher
@Jon Skeet: I want to get the value in DataRow. Is there any method? Instead of getting like list.ItemArray[0].Tenstrike
@Jon, FYI : dt.AsEnumerable().ToList()'System.Data.EnumerableRowCollection<System.Data.DataRow>' does not contain a definition for 'ToList'Gannie
@BabekoofCoder: Then you need using System.Linq; as normal for LINQ. It should be fine at that point. (I've added that into the answer.)Schaper
what if I want to convert it into a generic list of my custom type? List<MyType> = ?? Can this be done ?Pedigree
Is there a version of this for UWP? I can't get using system.data.DataSetExtensions in a UWP app... or DataTableExtensionsPhilippians
@Allstar: I haven't used UWP for doing anything with DataTables, I'm afraid.Schaper
@JonSkeet , thanks though I just got to it. Add the System.Data.DataSetExtensions nuget package... it then falls under the, already referenced, system.data namespace... which got me/confusedPhilippians
.AsEnumerable() exists in .net core 2.0 and above. stackoverflow.com/questions/3949302/…Gabel
@Sponsor: Yes, but not in .NET 3.0 and below - .NET Core 2.0 came much later than .NET 3.5. Note that this answer was written in 2008.Schaper
C
71
List<Employee> emp = new List<Employee>();

//Maintaining DataTable on ViewState
//For Demo only

DataTable dt = ViewState["CurrentEmp"] as DataTable;

//read data from DataTable 
//using lamdaexpression


emp = (from DataRow row in dt.Rows

   select new Employee
   {
       _FirstName = row["FirstName"].ToString(),
       _LastName = row["Last_Name"].ToString()

   }).ToList();
Cough answered 15/12, 2011 at 18:44 Comment(1)
Above code may not work bcs. dt.Rows has not implemented 'AsEnumerable'. This can be corrected as below: emp = (from DataRow row in dt.AsEnumerable() select new Employee { _FirstName = row["FirstName"].ToString(), _LastName = row["Last_Name"].ToString() }).ToList();Hedgerow
K
44

With C# 3.0 and System.Data.DataSetExtensions.dll,

List<DataRow> rows = table.Rows.Cast<DataRow>().ToList();
Kellum answered 16/10, 2008 at 13:28 Comment(1)
doing this has helped performance from just using a foreach over a datarow by 50% time.Langrage
S
37

You could use

List<DataRow> list = new List<DataRow>(dt.Select());

dt.Select() will return all rows in your table, as an array of datarows, and the List constructor accepts that array of objects as an argument to initially fill your list with.

Stripy answered 16/10, 2008 at 13:26 Comment(1)
Select() doesn't need any parameters. The parameterless overload would return all rows.Haleyhalf
G
18

You can create a extension function as :

public static List<T> ToListof<T>(this DataTable dt)
{
    const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
    var columnNames = dt.Columns.Cast<DataColumn>()
        .Select(c => c.ColumnName)
        .ToList();
    var objectProperties = typeof(T).GetProperties(flags);
    var targetList = dt.AsEnumerable().Select(dataRow =>
    {
        var instanceOfT = Activator.CreateInstance<T>();

        foreach (var properties in objectProperties.Where(properties => columnNames.Contains(properties.Name) && dataRow[properties.Name] != DBNull.Value))
        {
            properties.SetValue(instanceOfT, dataRow[properties.Name], null);
        }
        return instanceOfT;
    }).ToList();

    return targetList;
}


var output = yourDataInstance.ToListof<targetModelType>();
Generalization answered 16/10, 2008 at 13:24 Comment(2)
Doesn't work - see dotnetfiddle.net/I22r2c It should also be noted that using Reflection is slow and not reccomended in performance critical code.Drome
You need to add data type information for columns. DataTable dt = new DataTable(); dt.Columns.Add("id",typeof(Int32 )); dt.Columns.Add("name",typeof(String)); dt.Columns.Add("foo",typeof(DateTime )); for(int i=0;i<=1000;i++){dt.Rows.Add(i, "foo", DateTime.Now);}Generalization
M
17

If you just want a list of values from the "ID" int field returned, you could use...

List<int> ids = (from row in dt.AsEnumerable() select Convert.ToInt32(row["ID"])).ToList();
Madden answered 10/10, 2012 at 14:56 Comment(0)
I
15

I have added some modification to the code from this answer (https://mcmap.net/q/20354/-how-do-you-convert-a-datatable-into-a-generic-list) because for nullable Types it will return exception

public static List<T> DataTableToList<T>(this DataTable table) where T: new()
{
    List<T> list = new List<T>();
    var typeProperties = typeof(T).GetProperties().Select(propertyInfo => new
        {
            PropertyInfo = propertyInfo,
            Type = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType
        }).ToList();

    foreach (var row in table.Rows.Cast<DataRow>())
    {
        T obj = new T();
        foreach (var typeProperty in typeProperties)
        {
            object value = row[typeProperty.PropertyInfo.Name];
            object safeValue = value == null || DBNull.Value.Equals(value)
                ? null
                : Convert.ChangeType(value, typeProperty.Type);

            typeProperty.PropertyInfo.SetValue(obj, safeValue, null);
        }
        list.Add(obj);
    }
    return list;
}
Inofficious answered 14/4, 2015 at 11:13 Comment(3)
I removed the class because it can work with struct too.Reddish
This works pretty well, good job. If anyone wants to play around with the code, I created a .net fiddle at this link: dotnetfiddle.net/mTKevyDrome
@Drome I have added small modification, it should a bit increase performanceInofficious
A
11
using System.Data;


var myEnumerable = myDataTable.AsEnumerable();

List<MyClass> myClassList =
    (from item in myEnumerable
     select new MyClass{
         MyClassProperty1 = item.Field<string>("DataTableColumnName1"),
         MyClassProperty2 = item.Field<string>("DataTableColumnName2")
    }).ToList();
Acrobat answered 31/7, 2012 at 6:0 Comment(0)
G
7

Again, using 3.5 you may do it like:

dt.Select().ToList()

BRGDS

Gallaway answered 15/3, 2010 at 13:17 Comment(0)
B
6
// this is better suited for expensive object creation/initialization
IEnumerable<Employee> ParseEmployeeTable(DataTable dtEmployees)
{
    var employees = new ConcurrentBag<Employee>();

    Parallel.ForEach(dtEmployees.AsEnumerable(), (dr) =>
    {
        employees.Add(new Employee() 
        {
            _FirstName = dr["FirstName"].ToString(),
            _LastName = dr["Last_Name"].ToString()
        });
    });

    return employees;
}
Being answered 23/7, 2013 at 19:59 Comment(0)
F
6

The Easiest way of Converting the DataTable into the Generic list of class

using Newtonsoft.Json;

var json = JsonConvert.SerializeObject(dataTable);
var model = JsonConvert.DeserializeObject<List<ClassName>>(json);
Fancie answered 23/1, 2020 at 12:49 Comment(0)
U
5

A more 'magic' way, and doesn't need .NET 3.5.

If, for example, DBDatatable was returning a single column of Guids (uniqueidentifier in SQL) then you could use:

Dim gList As New List(Of Guid)
gList.AddRange(DirectCast(DBDataTable.Select(), IEnumerable(Of Guid)))
Undernourished answered 30/3, 2009 at 15:59 Comment(0)
V
4
DataTable dt;   // datatable should contains datacolumns with Id,Name

List<Employee> employeeList=new List<Employee>();  // Employee should contain  EmployeeId, EmployeeName as properties

foreach (DataRow dr in dt.Rows)
{
    employeeList.Add(new Employee{EmployeeId=dr.Id,EmplooyeeName=dr.Name});
}
Village answered 6/12, 2012 at 5:47 Comment(0)
A
3

DataTable.Select() doesnt give the Rows in the order they were present in the datatable.

If order is important I feel iterating over the datarow collection and forming a List is the right way to go or you could also use overload of DataTable.Select(string filterexpression, string sort).

But this overload may not handle all the ordering (like order by case ...) that SQL provides.

Agma answered 16/10, 2008 at 13:24 Comment(0)
I
2
        /* This is a generic method that will convert any type of DataTable to a List 
         * 
         * 
         * Example :    List< Student > studentDetails = new List< Student >();  
         *              studentDetails = ConvertDataTable< Student >(dt);  
         *
         * Warning : In this case the DataTable column's name and class property name
         *           should be the same otherwise this function will not work properly
         */

The following are the two functions in which if we pass a DataTable and a user defined class. It will then return the List of that class with the DataTable data.

        public static List<T> ConvertDataTable<T>(DataTable dt)
        {
            List<T> data = new List<T>();
            foreach (DataRow row in dt.Rows)
            {
                T item = GetItem<T>(row);
                data.Add(item);
            }
            return data;
        }


        private static T GetItem<T>(DataRow dr)
        {
            Type temp = typeof(T);
            T obj = Activator.CreateInstance<T>();

            foreach (DataColumn column in dr.Table.Columns)
            {
                foreach (PropertyInfo pro in temp.GetProperties())
                {
                   //in case you have a enum/GUID datatype in your model
                   //We will check field's dataType, and convert the value in it.
                    if (pro.Name == column.ColumnName){                
                    try
                    {
                        var convertedValue = GetValueByDataType(pro.PropertyType, dr[column.ColumnName]);
                        pro.SetValue(obj, convertedValue, null);
                    }
                    catch (Exception e)
                    {         
                       //ex handle code                   
                        throw;
                    }
                        //pro.SetValue(obj, dr[column.ColumnName], null);
                }
                    else
                        continue;
                }
            }
            return obj;
        }

This method will check the datatype of field, and convert dataTable value in to that datatype.

    private static object GetValueByDataType(Type propertyType, object o)
    {
        if (o.ToString() == "null")
        {
            return null;
        }
        if (propertyType == (typeof(Guid)) || propertyType == typeof(Guid?))
        {
            return Guid.Parse(o.ToString());
        }
        else if (propertyType == typeof(int) || propertyType.IsEnum) 
        {
            return Convert.ToInt32(o);
        }
        else if (propertyType == typeof(decimal) )
        {
            return Convert.ToDecimal(o);
        }
        else if (propertyType == typeof(long))
        {
            return Convert.ToInt64(o);
        }
        else if (propertyType == typeof(bool) || propertyType == typeof(bool?))
        {
            return Convert.ToBoolean(o);
        }
        else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
        {
            return Convert.ToDateTime(o);
        }
        return o.ToString();
    }

To call the preceding method, use the following syntax:

List< Student > studentDetails = new List< Student >();  
studentDetails = ConvertDataTable< Student >(dt); 

Change the Student class name and dt value based on your requirements. In this case the DataTable column's name and class property name should be the same otherwise this function will not work properly.

Inesinescapable answered 30/1, 2018 at 9:36 Comment(1)
Thanks for your answer. It would be great if you would also add a short explanation and a few comments to your code. This helps people to understand your answer better.Gaby
C
2
lPerson = dt.AsEnumerable().Select(s => new Person()
        {
            Name = s.Field<string>("Name"),
            SurName = s.Field<string>("SurName"),
            Age = s.Field<int>("Age"),
            InsertDate = s.Field<DateTime>("InsertDate")
        }).ToList();

Link to working DotNetFiddle Example

    using System;
    using System.Collections.Generic;   
    using System.Data;
    using System.Linq;
    using System.Data.DataSetExtensions;

    public static void Main()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("SurName", typeof(string));
        dt.Columns.Add("Age", typeof(int));
        dt.Columns.Add("InsertDate", typeof(DateTime));

        var row1= dt.NewRow();
        row1["Name"] = "Adam";
        row1["SurName"] = "Adam";
        row1["Age"] = 20;
        row1["InsertDate"] = new DateTime(2020, 1, 1);
        dt.Rows.Add(row1);

        var row2 = dt.NewRow();
        row2["Name"] = "John";
        row2["SurName"] = "Smith";
        row2["Age"] = 25;
        row2["InsertDate"] = new DateTime(2020, 3, 12);
        dt.Rows.Add(row2);

        var row3 = dt.NewRow();
        row3["Name"] = "Jack";
        row3["SurName"] = "Strong";
        row3["Age"] = 32;
        row3["InsertDate"] = new DateTime(2020, 5, 20);
        dt.Rows.Add(row3);

        List<Person> lPerson = new List<Person>();
        lPerson = dt.AsEnumerable().Select(s => new Person()
        {
            Name = s.Field<string>("Name"),
            SurName = s.Field<string>("SurName"),
            Age = s.Field<int>("Age"),
            InsertDate = s.Field<DateTime>("InsertDate")
        }).ToList();
        
        foreach(Person pers in lPerson)
        {
            Console.WriteLine("{0} {1} {2} {3}", pers.Name, pers.SurName, pers.Age, pers.InsertDate);
        }
    }   
    
    public class Person
    {
        public string Name { get; set; }
        public string SurName { get; set; }
        public int Age { get; set; }
        public DateTime InsertDate { get; set; }
    }
}
Cycle answered 6/5, 2020 at 13:12 Comment(0)
A
1

Use System.Data namespace then you will get .AsEnumerable().

Aporia answered 20/4, 2015 at 6:34 Comment(0)
W
1

We can use a Generic Method for converting DataTable to List instead of manually converting a DataTable to List.

Note: DataTable's ColumnName and Type's PropertyName should be same.

Call the below Method:

long result = Utilities.ConvertTo<Student>(dt ,out listStudent);

// Generic Method
public class Utilities
{
    public static long ConvertTo<T>(DataTable table, out List<T> entity)
    {
        long returnCode = -1;
        entity = null;

        if (table == null)
        {
            return -1;
        }

        try
        {
            entity = ConvertTo<T>(table.Rows);
            returnCode = 0;
        }

        catch (Exception ex)
        {
            returnCode = 1000;
        }

        return returnCode;
    }

    static List<T> ConvertTo<T>(DataRowCollection rows)
    {
        List<T> list = null;
        if (rows != null)
        {
            list = new List<T>();

            foreach (DataRow row in rows)
            {
                T item = CreateItem<T>(row);
                list.Add(item);
            }
        }

        return list;
    }

    static T CreateItem<T>(DataRow row)
    {
        string str = string.Empty;
        string strObj = string.Empty;

        T obj = default(T);

        if (row != null)
        {
            obj = Activator.CreateInstance<T>();
            strObj = obj.ToString();
            NameValueCollection objDictionary = new NameValueCollection();

            foreach (DataColumn column in row.Table.Columns)
            {
                PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);

                if (prop != null)
                {
                    str = column.ColumnName;

                    try
                    {
                        objDictionary.Add(str, row[str].ToString());
                        object value = row[column.ColumnName];
                        Type vType = obj.GetType();

                        if (value == DBNull.Value)
                        {
                            if (vType == typeof(int) || vType == typeof(Int16)
                                                     || vType == typeof(Int32)
                                                     || vType == typeof(Int64)
                                                     || vType == typeof(decimal)
                                                     || vType == typeof(float)
                                                     || vType == typeof(double))
                            {
                                value = 0;
                            }

                            else if (vType == typeof(bool))
                            {
                                value = false;
                            }

                            else if (vType == typeof(DateTime))
                            {
                                value = DateTime.MaxValue;
                            }

                            else
                            {
                                value = null;
                            }

                            prop.SetValue(obj, value, null);
                        }

                        else
                        {
                            prop.SetValue(obj, value, null);
                        }
                    }

                    catch(Exception ex)
                    {

                    }
                }
            }

            PropertyInfo ActionProp = obj.GetType().GetProperty("ActionTemplateValue");

            if (ActionProp != null)
            {
                object ActionValue = objDictionary;
                ActionProp.SetValue(obj, ActionValue, null);
            }
        }

        return obj;
    }
}
Whirlybird answered 10/3, 2016 at 11:58 Comment(0)
E
1

If anyone want's to create custom function to convert datatable to list

class Program
{
    static void Main(string[] args)
    {
        DataTable table = GetDataTable();
        var sw = new Stopwatch();

        sw.Start();
        LinqMethod(table);
        sw.Stop();
        Console.WriteLine("Elapsed time for Linq Method={0}", sw.ElapsedMilliseconds);

        sw.Reset();

        sw.Start();
        ForEachMethod(table);
        sw.Stop();
        Console.WriteLine("Elapsed time for Foreach method={0}", sw.ElapsedMilliseconds);

        Console.ReadKey();
    }

    private static DataTable GetDataTable()
    {
        var table = new DataTable();
        table.Columns.Add("ID", typeof(double));
        table.Columns.Add("CategoryName", typeof(string));
        table.Columns.Add("Active", typeof(double));

        var rand = new Random();

        for (int i = 0; i < 100000; i++)
        {
            table.Rows.Add(i, "name" + i,  rand.Next(0, 2));
        }
        return table;
    }

    private static void LinqMethod(DataTable table)
    {
        var list = table.AsEnumerable()
            .Skip(1)
            .Select(dr =>
                new Category
                {
                    Id = Convert.ToInt32(dr.Field<double>("ID")),
                    CategoryName = dr.Field<string>("CategoryName"),                      
                    IsActive =
                            dr.Field<double>("Active") == 1 ? true : false
                }).ToList();
    }
    private static void ForEachMethod(DataTable table)
    {
        var categoryList = new List<Category>(table.Rows.Count);
        foreach (DataRow row in table.Rows)
        {
            var values = row.ItemArray;
            var category = new Category()
            {
                Id = Convert.ToInt32(values[0]),
                CategoryName = Convert.ToString(values[1]),                   
                IsActive = (double)values[2] == 1 ? true : false
            };
            categoryList.Add(category);
        }
    }

    private class Category
    {
        public int Id { get; set; }
        public string CategoryName { get; set; }
        public bool IsActive { get; set; }
    }
}

If we execute above code, Foreach method finishes in 56ms while linq one takes 101ms ( for 1000 records). So Foreach method is better to use. Source:Ways to Convert Datatable to List in C# (with performance test example)

Epiphyte answered 23/10, 2020 at 15:48 Comment(0)
B
0

This worked for me: Need at least .Net Framework 3.5, Code below displays DataRow turned to Generic.IEnumerable, comboBox1 has been used for a better illustration.

using System.Linq;

DataTable dt = new DataTable();            
dt = myClass.myMethod();                 
List<object> list = (from row in dt.AsEnumerable() select (row["name"])).ToList();
comboBox1.DataSource = list;
Beavers answered 2/9, 2014 at 13:26 Comment(0)
I
0

Output

public class ModelUser
{
    #region Model

    private string _username;
    private string _userpassword;
    private string _useremail;
    private int _userid;

    /// <summary>
    /// 
    /// </summary>
    public int userid
    {
        set { _userid = value; }
        get { return _userid; }
    }


    /// <summary>
    /// 
    /// </summary>

    public string username
    {
        set { _username = value; }
        get { return _username; }
    }

    /// <summary>
    /// 
    /// </summary>
    public string useremail
    {
        set { _useremail = value; }
        get { return _useremail; }
    }

    /// <summary>
    /// 
    /// </summary>
    public string userpassword
    {
        set { _userpassword = value; }
        get { return _userpassword; }
    }
    #endregion Model
}

public List<ModelUser> DataTableToList(DataTable dt)
{
    List<ModelUser> modelList = new List<ModelUser>();
    int rowsCount = dt.Rows.Count;
    if (rowsCount > 0)
    {
        ModelUser model;
        for (int n = 0; n < rowsCount; n++)
        {
            model = new ModelUser();

            model.userid = (int)dt.Rows[n]["userid"];
            model.username = dt.Rows[n]["username"].ToString();
            model.useremail = dt.Rows[n]["useremail"].ToString();
            model.userpassword = dt.Rows[n]["userpassword"].ToString();

            modelList.Add(model);
        }
    }
    return modelList;
}

static DataTable GetTable()
{
    // Here we create a DataTable with four columns.
    DataTable table = new DataTable();
    table.Columns.Add("userid", typeof(int));
    table.Columns.Add("username", typeof(string));
    table.Columns.Add("useremail", typeof(string));
    table.Columns.Add("userpassword", typeof(string));

    // Here we add five DataRows.
    table.Rows.Add(25, "Jame", "[email protected]", DateTime.Now.ToString());
    table.Rows.Add(50, "luci", "[email protected]", DateTime.Now.ToString());
    table.Rows.Add(10, "Andrey", "[email protected]", DateTime.Now.ToString());
    table.Rows.Add(21, "Michael", "[email protected]", DateTime.Now.ToString());
    table.Rows.Add(100, "Steven", "[email protected]", DateTime.Now.ToString());
    return table;
}

protected void Page_Load(object sender, EventArgs e)
{
    List<ModelUser> userList = new List<ModelUser>();

    DataTable dt = GetTable();

    userList = DataTableToList(dt);

    gv.DataSource = userList;
    gv.DataBind();
}[enter image description here][1]

</asp:GridView>
</div>
Isomerism answered 7/2, 2016 at 9:1 Comment(0)
L
0

You can use a generic method like that for datatable to generic list

public static List<T> DataTableToList<T>(this DataTable table) where T : class, new()
{
    try
    {
        List<T> list = new List<T>();

        foreach (var row in table.AsEnumerable())
        {
            T obj = new T();

            foreach (var prop in obj.GetType().GetProperties())
            {
                try
                {
                    PropertyInfo propertyInfo = obj.GetType().GetProperty(prop.Name);
                    if (propertyInfo.PropertyType.IsEnum)
                    {
                        propertyInfo.SetValue(obj, Enum.Parse(propertyInfo.PropertyType, row[prop.Name].ToString()));
                    }
                    else
                    {
                        propertyInfo.SetValue(obj, Convert.ChangeType(row[prop.Name], propertyInfo.PropertyType), null);
                    }                          
                }
                catch
                {
                    continue;
                }
            }

            list.Add(obj);
        }

        return list;
    }
    catch
    {
        return null;
    }
}
Lactose answered 18/9, 2018 at 16:3 Comment(0)
M
0

Converting DataTable to Generic Dictionary

public static Dictionary<object,IList<dynamic>> DataTable2Dictionary(DataTable dt)
{
    Dictionary<object, IList<dynamic>> dict = new Dictionary<dynamic, IList<dynamic>>();

    foreach(DataColumn column in dt.Columns)
    {
        IList<dynamic> ts = dt.AsEnumerable()
                              .Select(r => r.Field<dynamic>(column.ToString()))
                              .ToList();
        dict.Add(column, ts);
    }
    return dict;
}
Manzanilla answered 6/6, 2019 at 6:44 Comment(0)
E
0

Use Extension :

public static class Extensions
{
    #region Convert Datatable To List
    public static IList<T> ToList<T>(this DataTable table) where T : new()
    {
        IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
        IList<T> result = new List<T>();

        foreach (var row in table.Rows)
        {
            var item = CreateItemFromRow<T>((DataRow)row, properties);
            result.Add(item);
        }
        return result;
    }
    private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
    {
        T item = new T();
        foreach (var property in properties)
        {
            property.SetValue(item, row[property.Name], null);
        }
        return item;
    }
    #endregion
}
Extrasystole answered 17/11, 2019 at 10:28 Comment(0)
Q
0

you can use following two Generic functions

private static List<T> ConvertDataTable<T>(DataTable dt)
    {
        List<T> data = new List<T>();
        foreach (DataRow row in dt.Rows)
        {
            T item = GetItem<T>(row);
            data.Add(item);
        }
        return data;
    }
    private static T GetItem<T>(DataRow dr)
    {

        Type temp = typeof(T);
        T obj = Activator.CreateInstance<T>();

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

and use it as following

List<StudentScanExamsDTO> studentDetails = ConvertDataTable<StudentScanExamsDTO>(dt);
Quaver answered 29/1, 2020 at 12:10 Comment(0)
M
0

try this using Newtonsoft Json:

var json = JsonConvert.SerializeObject(dataTable);
var YourConvertedDataType = JsonConvert.DeserializeObject<YourDataType>(json);
Minus answered 30/10, 2021 at 11:16 Comment(0)
B
0

To get List of values instead of ItemArray, do this:

List<string> s = dt.AsEnumerable().Select(x => x[0].ToString()).ToList();

The above assumes that you want list of string values from column 0.

Bugle answered 12/3, 2022 at 2:21 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Gratia
F
-1

To assign the DataTable rows to the generic List of class

  List<Candidate> temp = new List<Candidate>();//List that holds the Candidate Class,
    //Note:The Candidate class contains RollNo,Name and Department
    //tb is DataTable
    temp = (from DataRow dr in tb.Rows
                        select new Candidate()
                        {
                            RollNO = Convert.ToInt32(dr["RollNO"]),
                            Name = dr["Name"].ToString(),
                            Department = dr["Department"].ToString(),

                        }).ToList();
Fancie answered 19/12, 2019 at 10:28 Comment(0)
B
-1

To convert a dataTable into a generic list, here List<CustomerEntity>, try this code:

List<CustomerEntity> customers = new List<CustomerEntity>();
List<DataRow> rows = dataTable.AsEnumerable().ToList();

rows.ForEach(r =>
    {
        customers.Add(new CustomerEntity()
        {
            Id= r.Field<int>("Id"),
            Name= r.Field<string>("Name"),
            Address= r.Field<string>("Address"),
            Point= r.Field<int>("Point")
        }
    );
    });

or

List<CustomerEntity> customers = dataTable.Rows.OfType<DataRow>()
        .Select(dr => new CustomerEntity
        {
            Id= dr.Field<int>("Id"),
            Name= dr.Field<string>("Name"),
            Address= dr.Field<string>("Address"),
            Point= dr.Field<int>("Point")
        }).ToList();
Bottomless answered 18/6, 2023 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.