Wednesday, 2 October 2013

binding datatable columns into class model

binding datatable columns into class model

I have a DataTable and a Model. Are there any other ways on how to map the
columns on the members of the class? Let me illustrate my current way,
class Model
private class MyModel
{
public int ModelID { get; set; }
public string ModelName { get; set; }
}
and a DataTable with the following data column,
ModelID | ModelName
The records f the Datatable is from teh result of my sql query.
SELECT ModelID, ModelName FROM tableName
Now, when I map the records to IList<MyModel> myList, I do a foreach loop.
foreach (DataRow row in MyDataTable)
{
MyModel model = new MyModel()
model.ModelID = Convert.ToInt32(row["Position"].ToString());
model.ModelName = row["ModelName"].ToString();
myList.Add(model);
}
and this totally works fine. This is not really a problem but it is very
time consuming considering that i have many data tables and has 20 columns
or more, and many more classes.
What I am looking for is if there are other ways to do it. Like an
existing class but I really don't know what's its name.
If this question has already been asked, please help me find the link.
Thank you.

No comments:

Post a Comment