Converting DataTable To Model List

In this blog, we will discuss the simple common solution to convert an object of type DataTable into a list of Models in C#. I have tried my best to explain the use of the common generic method to convert any datable to Model List. I hope this would be helpful in the situations where we need to set the value of each property of a model with the column values of Datatable using a "For" loop.

Requirement

There might be a situation where you have a model named "Student" and you want to show the list of students on your page. You have the result from SQL in Datatable named "ResultDT". Similarly, you may have a number of other models and DataTables.

If your Student Model is like below, then your data table should also have the same column name with same datatypes. That means your result in "ResultDT" should have at least these 3 columns - StudentId, RoleNumber, Name.
  1. public class Student  
  2. {  
  3.    public int StudentId { get; set; }  
  4.    public int RoleNumber { get; set; }  
  5.    public string Name { get; set; }  
  6. }  
Need of Generic method to convert datatable to model list

Generally, we have to set the property of model one by one within for each or we have to use some tools or plugins for mapping with the properties. So this generic method is needed because:
  • To avoid writing lot of code to set properties of the model
  • A generic method would be re-usable for all Models and data tables
  • It's clean and less code would be required.
Generic Method

Here is the generic method that you can add to any of your common files from where you can call this whenever required to convert any data table to the model list. Suppose, the name of the class is CommonMethod in which this method exists.
  1. public static class CommonMethod {  
  2.     public static List < T > ConvertToList < T > (DataTable dt) {  
  3.         var columnNames = dt.Columns.Cast < DataColumn > ().Select(c => c.ColumnName.ToLower()).ToList();  
  4.         var properties = typeof(T).GetProperties();  
  5.         return dt.AsEnumerable().Select(row => {  
  6.             var objT = Activator.CreateInstance < T > ();  
  7.             foreach(var pro in properties) {  
  8.                 if (columnNames.Contains(pro.Name.ToLower())) {  
  9.                     try {  
  10.                         pro.SetValue(objT, row[pro.Name]);  
  11.                     } catch (Exception ex) {}  
  12.                 }  
  13.             }  
  14.             return objT;  
  15.         }).ToList();  
  16.     }  
  17. }  
Example

Below is the function to get the list of students in which we are using the generic method to convert our data table result to List of Model "Student".
  1. public List < Student > GetStudentList() {  
  2.     Datatable ResultDT = new DataTable();  
  3.     ResultDT = _DataBAL.GetData(); // Call BusinessLogic to fill DataTable, Here your ResultDT will get the result in which you will be having single or multiple rows with columns "StudentId,RoleNumber and Name"  
  4.     List < Student > Studentlist = new List < Student > ();  
  5.     Studentlist = CommonMethod.ConvertToList < Student > (ResultDT);  
  6.     return Studentlist;  
  7. }  
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now