Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. datatable to object array

datatable to object array

Scheduled Pinned Locked Moved C#
questioncsharpdata-structures
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ramkithepower
    wrote on last edited by
    #1

    I have a table that would be read into the dataset using dataadapter.fill method. The table will be of the below format Name | Age | Score | Gender Ram 14 75 M Sam 15 85 M John 16 95 M I have an object of the following sort in c# public class students { //initializing private variables public string Name { //get/set } public string Age { //get/set } public string Score { //get/set } } How can i convert this table in the dataset to an array of Students object?

    Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

    _ P 2 Replies Last reply
    0
    • R Ramkithepower

      I have a table that would be read into the dataset using dataadapter.fill method. The table will be of the below format Name | Age | Score | Gender Ram 14 75 M Sam 15 85 M John 16 95 M I have an object of the following sort in c# public class students { //initializing private variables public string Name { //get/set } public string Age { //get/set } public string Score { //get/set } } How can i convert this table in the dataset to an array of Students object?

      Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

      _ Offline
      _ Offline
      _Erik_
      wrote on last edited by
      #2

      There are many ways. You might encapsulate Data objects within your classes, and maybe getting a IEnumerable<Student> could be a better approach:

      public class Student
      {
      DataRow dr;

      internal Student(DataRow dr)
      {
          this.dr = dr;
      }
      
      public string Name
      {
          get { return dr\["Name"\].ToString(); }
          set { dr\["Name"\] = value; }
      }
      
      public string Age
      {
          get { return dr\["Age"\].ToString(); }
          set { dr\["Age"\] = value; }
      }
      
      public string Score
      {
          get { return dr\["Score"\].ToString(); }
          set { dr\["Score"\] = value; }
      }
      
      // And more properties as needed
      

      }

      public class StudentCollection
      {
      public IEnumerable<Student> GetStudents()
      {
      // Once you've got the DataTable, called "dt" for example
      foreach (DataRow dr in dt.Rows)
      yield return new Student(dr);
      }
      }

      modified on Wednesday, December 15, 2010 11:35 AM

      1 Reply Last reply
      0
      • R Ramkithepower

        I have a table that would be read into the dataset using dataadapter.fill method. The table will be of the below format Name | Age | Score | Gender Ram 14 75 M Sam 15 85 M John 16 95 M I have an object of the following sort in c# public class students { //initializing private variables public string Name { //get/set } public string Age { //get/set } public string Score { //get/set } } How can i convert this table in the dataset to an array of Students object?

        Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        If you want the data in an array of some class why bother reading it into a DataTable? That just wastes cycles. Cut out the middleman; use a DataReader to populate your array.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups