Friday, April 15, 2016

  • Conevert Datatable to Object Array C# ASP.Net / ASP MVC

    We need to convert the Datatable dt to Array of objects StudentListResponse



     DataTable dt = new DataTable();
     SqlDataAdapter da = new SqlDataAdapter(command);
     da.Fill(dt);

                var list = dt.AsEnumerable()
    .Skip(1)
    .Select(dr =>
            new StudentListResponse
            {
                id = dr["PKStudentID"].ToString(),
                name = dr["StudentName"].ToString(),
                profile_pic = dr["PhotoURL"].ToString()
            }
            ).ToList();



                StudentListResponse[] students = list.ToArray();

    Its very simple as explained in the previous post to convert Datatable to Object list. Just convert the list to Array as highlighted above.



  • 0 comments:

    Post a Comment

    Copyright @ 2013 Code Snippets.