30 April 2024

Convert DataTable to List In C#

 Convert DataTable to List In C#

You can achieve in below three ways

Using a Loop.
Using LINQ.
Using a Generic Method.

Using Linq
List<Student> studentList = new List<Student>();
    studentList = (from DataRow dr in dt.Rows
            select new Student()
            {
                StudentId = Convert .ToInt32 (dr["StudentId"]),
                StudentName = dr["StudentName"].ToString(),
                Address = dr["Address"].ToString(),
                MobileNo = dr["MobileNo"].ToString()
            }).ToList();

No comments:

Post a Comment

Comments Welcome

Benefits of microservices

Benefits of microservices      1. Development & Team Agility Architecture- and language-neutral: Teams aren't locked into one progr...