Convert DataTable into enity object/class

Posted by Soft.Prof under ASP.NET on 6/5/2014 | Points: 10 | Views : 5220 | Status : [Member] | Replies : 1
how to convert DataTable into entity object/class




Responses

Posted by: kgovindarao523-21772 on: 6/5/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
Try like this.
MyEntity is the class with some properties associated with your DataTable .
public class MyEntity
{
public string Name {get; set;}
public string Type {get; set;}
public Load(object [] array)
{
this.Name = array[0].ToString();
this.Type = array[1].ToString();
}
}

To convert DataTable Rows into List of entity objects, here is the code.
List<MyEntity> list = new List<MyEntity>();//
foreach(DataRow dr in table.Rows)//table is your data table
{
MyEntity obj = new MyEntity();
obj.Load(dr.ItemArray);
list.Add(obj);
}


Thank you,
Govind

Soft.Prof, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response