I am trying to map datatable row to object. (C#)
I have Datatable like this.
|----|------|---------|
|ID | Name | Address |
|----|------|---------|
| 1 | Tom | USA |
|----|------|---------|
| 2 | Tim | AU |
|----|------|---------|
And I have Object
Contact {
int ID,
String Name,
String Address
}
And I want to map dynamically like
Contact contact = new Contact();
foreach (var row in rows) {
foreach(var col in table.Columns)
contact[col.ColumnName] = row[col.ColumnaName]
}
Like Javascript, Is there any way to set value in object by columnName?