Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I am new to .net and I got an internship in a company.
To complete my task I need to convert the datatable into the entity.

Can any one please give me code so that I can convert the datatable into the entity?
Posted
Updated 12-May-11 22:08pm
v2
Comments
Dalek Dave 13-May-11 4:08am    
Edited for Grammar and Readability.

If by "entity" you mean instances of the relevant class, then sorry - without knowing a lot more about the class you need to create, and the format of your datatable it is really not possible to be so specific as to give you code!
 
Share this answer
 
Comments
MURARI00 13-May-11 3:05am    
thanks for response as u told u give me the full code as sample for converting the datatable into the particlular class object please
OriginalGriff 13-May-11 3:28am    
Read my reply.
Do I know your classes? No.
Do I know your data structure? No.
So how can you expect me to know how to create one from the other?

It's like trying to forge your drivers licence, given only all the letters of the alphabet!
Dalek Dave 13-May-11 4:08am    
Good point Griff.
Try this,

You can Declare your own class with your own properties and load it's values from your Datatable like this,

C#
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

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


I hope this will help you.
 
Share this answer
 
Comments
Dalek Dave 13-May-11 4:08am    
Good Call
MURARI00 13-May-11 4:20am    
Thanks and can you give me the code to create the dynamic entities from data table
MURARI00 13-May-11 5:06am    
HI can u give a example for this article please http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=157601&av=232018

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900