If you are developing an ASP.NET Core application, you may come across a situation where you need to convert Datatable to Object Model. In this article, we will learn How to Convert Datatable to Object Model in ASP.NET Core MVC C#.
Install Required Software
What exactly is a DataTable?
A DataTable is a .NET Framework class that represents a datatable in memory. It is made up of a collection of DataRow objects, each representing a single row in the database. The table’s columns are defined using DataColumn objects.
What exactly is an Object Model?
The model is a standard C# class. The model is responsible for handling data and business logic. A model represents the data’s shape. The model is responsible for handling database-related changes.
Why Should You Convert a DataTable to an Object Model?
Converting a DataTable to an Object Model might make working with data in your application easier. When you have an Object Model, you may access data using strongly typed properties and methods, which makes your code more maintainable and error-prone.
DataTable to Object Model Conversion in C#
To represent the entity you wish to convert the DataTable to, create a class. Connect the DataTable columns to the class’s attributes. For each row in the DataTable, create an instance of the class, and fill the attributes with the information from the appropriate columns.
ASP.NET Core C#’s DataTable to Object Model Conversion
Let’s look at an example of how to do this in ASP.NET Core C# with a DataTable. Assume you have a DataTable containing a list of items, each with an Id, firstname, lastname, job, salary, and hiredate.
Create Model
namespace Asp.netcore_Tutorials.Models { public class Employee { public int EmpId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string job { get; set; } public float amount { get; set; } public DateTime tdate { get; set; } } }
Create Controller
using Asp.netcore_Tutorials.Models; using Microsoft.AspNetCore.Mvc; using System.Data; namespace Asp.netcore_Tutorials.Controllers { public class DatatabletoModelController : Controller { public IActionResult Index() { var datatable = new DataTable(); datatable.Columns.Add("EmpId", typeof(int)); datatable.Columns.Add("firstname",type:typeof(string)); datatable.Columns.Add("lastname",type:typeof(string)); datatable.Columns.Add("job", type: typeof(string)); datatable.Columns.Add("amount", type: typeof(string)); datatable.Columns.Add("tdate", type: typeof(DateTime)); datatable.Rows.Add(1,"John ","Stephen","Manager","150000","10-01-2023"); datatable.Rows.Add(1, "Vicky ", "Poiting", "Software Developer", "10000", "10-02-2022"); datatable.Rows.Add(1, "Nathan", "Mick", "Tester", "70000", "10-11-2022"); List<Employee> custlist = new List<Employee>(); custlist = (from DataRow dr in datatable.Rows select new Employee() { EmpId = Convert.ToInt32(dr["EmpId"]), FirstName = dr["firstname"].ToString(), LastName = dr["lastname"].ToString(), job = dr["job"].ToString(), amount = Convert.ToInt64(dr["amount"]), tdate = Convert.ToDateTime(dr["tdate"].ToString()) }).ToList(); return View(custlist); } } }
In this example, a class called Employee is created to represent an entity for an Employee, complete with an id, firstname,lastname, job,salary, and hiredate. Then using the same columns and some test data, we create a DataTable. The last step involves iterating through the DataTable’s rows, creating instances of the Employee class for each row, and filling each instance’s attributes with information from the appropriate columns. The Employee class instances are then each added to a list of Employees.
Previous Article Also check Populate DropdownList with Month Names Using LINQ
Create View
@* For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 *@ @model IEnumerable<Asp.netcore_Tutorials.Models.Employee> @{ ViewData["Title"] = "Home Page"; } <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <h1>DataTable to Model</h1> <table class="table table-primary"> <thead> <tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Job</th> <th>Salary</th> <th>Date</th> </tr> </thead> @foreach(var custdr in Model) { <tbody> <tr> <td>@custdr.EmpId</td> <td>@custdr.FirstName</td> <td>@custdr.LastName</td> <td>@custdr.job</td> <td>@custdr.amount</td> <td>@custdr.tdate</td> </tr> </tbody> } </table>
Conclusion
In this article, we covered how to change an ASP.NET Core DataTable into an object model. It can be simpler to work with the data in your application and more manageable and error-free to write code by converting a DataTable to an Object Model. You should be able to convert DataTables to Object Models in your own ASP.NET Core apps by following the instructions provided in this article.