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

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...