Search code examples
c#entitystrongly-typed-dataset

How to convert typed DataTable into List of Entity?


I need to convert the data of a Typed DataTable to List of my entity. I've Mapped all the fields of entity with field of DataTable.

Any ideas?


Solution

  • One way could be by doing it through custom code Lets suppose you Entity class name is 'MyEnt'

     public class MyEnt
        {
         public string Name {get; set;}
         public string Type {get; set;}
         public LoadMyEnt(object [] array)
          {
             this.Name = array[0].ToString();
             this.Type = array[1].ToString();   
          }
        }
    

    //for datatable, you could do

    List<MyEnt> list = new List<MyEnt>();
    foreach(DataRow dr in table.Rows)
    {
      MyEnt obj = new MyEnt();
    obj.Load(dr.ItemArray);
    list.Add(obj);
    }