Thursday, 26 December 2019

Convert DataTable to List In C#

  1. Using a Loop.
  1. Using LINQ.
  1. Using a Generic Method.
Convert DataTable to List Using a Loop

DataTable dt = new DataTable("Student"); 
dt.Columns.Add("StudentId", typeof(Int32)); 
dt.Columns.Add("StudentName", typeof(string)); 
dt.Columns.Add("Address", typeof(string)); 
dt.Columns.Add("MobileNo", typeof(string)); 
    //Data 
dt.Rows.Add(1, "Manish", "Hyderabad","0000000000"); 
dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111"); 
dt.Rows.Add(3, "Namit", "Pune", "1222222222"); 
dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333");
public void StudentList() 

    //  DataTable dt = new DataTable("Branches"); 
    DataTable dt = new DataTable("Student"); 
    dt.Columns.Add("StudentId", typeof(Int32)); 
    dt.Columns.Add("StudentName", typeof(string)); 
    dt.Columns.Add("Address", typeof(string)); 
    dt.Columns.Add("MobileNo", typeof(string)); 
    //Data 
    dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000"); 
    dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111"); 
    dt.Rows.Add(3, "Namit", "Pune", "1222222222"); 
    dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333"); 
 
    List<Student> studentList = new List<Student>(); 
    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
        Student student = new Student(); 
        student.StudentId = Convert .ToInt32 (dt.Rows[i]["StudentId"]); 
        student.StudentName = dt.Rows[i]["StudentName"].ToString(); 
        student.Address = dt.Rows[i]["Address"].ToString(); 
        student.MobileNo = dt.Rows[i]["MobileNo"].ToString(); 
        studentList.Add(student); 
    } 
}

Convert DataTable to List Using Linq


public void StudentListUsingLink() 

    //  DataTable dt = new DataTable("Branches"); 
    DataTable dt = new DataTable("Student"); 
    dt.Columns.Add("StudentId", typeof(Int32)); 
    dt.Columns.Add("StudentName", typeof(string)); 
    dt.Columns.Add("Address", typeof(string)); 
    dt.Columns.Add("MobileNo", typeof(string)); 
    //Data 
    dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000"); 
    dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111"); 
    dt.Rows.Add(3, "Namit", "Pune", "1222222222"); 
    dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333"); 
    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(); 
   

Convert DataTable to List using a Generic Method

private static List<T> ConvertDataTable<T>(DataTable dt) 

    List<T> data = new List<T>(); 
    foreach (DataRow row in dt.Rows) 
    { 
        T item = GetItem<T>(row); 
        data.Add(item); 
    } 
    return data; 

private static T GetItem<T>(DataRow dr) 

    Type temp = typeof(T); 
    T obj = Activator.CreateInstance<T>(); 
 
    foreach (DataColumn column in dr.Table.Columns) 
    { 
        foreach (PropertyInfo pro in temp.GetProperties()) 
        { 
            if (pro.Name == column.ColumnName) 
                pro.SetValue(obj, dr[column.ColumnName], null); 
            else 
                continue; 
        } 
    } 
    return obj; 
}
List< Student > studentDetails = new List< Student >(); 
studentDetails = ConvertDataTable< Student >(dt);
x

No comments:

Post a Comment