Introduction
This article explains various ways to convert a DataTable to a List in C#. There are the
following 3 ways to convert a DataTable to a List.
1. Using a Loop.
2. Using LINQ.
3. Using a Generic Method.
For this example I am creating a simple Student class like:
01. public class Student
02. {
03. public int StudentId { get; set; }
04. public string StudentName { get; set; }
05. public string Address { get; set; }
06. public string MobileNo { get; set; }
07. }
And a DataTable with some data like:
01. DataTable dt = new DataTable("Student");
02. dt.Columns.Add("StudentId", typeof(Int32));
03. dt.Columns.Add("StudentName", typeof(string));
04. dt.Columns.Add("Address", typeof(string));
05. dt.Columns.Add("MobileNo", typeof(string));
06. //Data
07. dt.Rows.Add(1, "Manish", "Hyderabad","0000000000");
08. dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111");
09. dt.Rows.Add(3, "Namit", "Pune", "1222222222");
10. dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333");
Now I will convert the receding DataTable into a List< Student > using all the preceding
three methods.
Convert DataTable to List Using a Loop
In this method I am using a simple for loop; other loops can also be used.
01. public void StudentList()
02. {
03. // DataTable dt = new DataTable("Branches");
04. DataTable dt = new DataTable("Student");
05. dt.Columns.Add("StudentId", typeof(Int32));
06. dt.Columns.Add("StudentName", typeof(string));
07. dt.Columns.Add("Address", typeof(string));
08. dt.Columns.Add("MobileNo", typeof(string));
09. //Data
10. dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000");
11. dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111");
12. dt.Rows.Add(3, "Namit", "Pune", "1222222222");
13. dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333");
14.
15. List<Student> studentList = new List<Student>();
16. for (int i = 0; i < dt.Rows.Count; i++)
17. {
18. Student student = new Student();
19. student.StudentId = Convert .ToInt32 (dt.Rows[i]
["StudentId"]);
20. student.StudentName = dt.Rows[i]
["StudentName"].ToString();
21. student.Address = dt.Rows[i]["Address"].ToString();
22. student.MobileNo = dt.Rows[i]["MobileNo"].ToString();
23. studentList.Add(student);
24. }
25. }
Convert DataTable to List Using Linq
This is the modern approach for creating a List in C#.
01. public void StudentListUsingLink()
02. {
03. // DataTable dt = new DataTable("Branches");
04. DataTable dt = new DataTable("Student");
05. dt.Columns.Add("StudentId", typeof(Int32));
06. dt.Columns.Add("StudentName", typeof(string));
07. dt.Columns.Add("Address", typeof(string));
08. dt.Columns.Add("MobileNo", typeof(string));
09. //Data
10. dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000");
11. dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111");
12. dt.Rows.Add(3, "Namit", "Pune", "1222222222");
13. dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333");
14. List<Student> studentList = new List<Student>();
15. studentList = (from DataRow dr in dt.Rows
16. select new Student()
17. {
18. StudentId = Convert .ToInt32 (dr["StudentId"]),
19. StudentName = dr["StudentName"].ToString(),
20. Address = dr["Address"].ToString(),
21. MobileNo = dr["MobileNo"].ToString()
22. }).ToList();
23.
24. }
Note: The advantage of the preceding two method is we can something.
Convert DataTable to List using a Generic Method
This is a generic method that will convert any type of DataTable to a List (the DataTable
structure and List class structure should be the same).
The following are the two functions in which if we pass a DataTable and a user de�ned
class. It will then return the List of that class with the DataTable data.
01. private static List<T> ConvertDataTable<T>(DataTable dt)
02. {
03. List<T> data = new List<T>();
04. foreach (DataRow row in dt.Rows)
05. {
06. T item = GetItem<T>(row);
07. data.Add(item);
08. }
09. return data;
10. }
11. private static T GetItem<T>(DataRow dr)
12. {
13. Type temp = typeof(T);
14. T obj = Activator.CreateInstance<T>();
15.
16. foreach (DataColumn column in dr.Table.Columns)
17. {
18. foreach (PropertyInfo pro in temp.GetProperties())
19. {
20. if (pro.Name == column.ColumnName)
21. pro.SetValue(obj, dr[column.ColumnName], null);
22. else
23. continue;
24. }
25. }
26. return obj;
27. }
To call the preceding method, use the following syntax:
01. List< Student > studentDetails = new List< Student >();
02. studentDetails = ConvertDataTable< Student >(dt);