DataTable
. DataTable
into a JSON object.
All of these answers are really great for moving data! Where they fail is preserving the column type of data being moved. This becomes a problem when you want to do things like merge datatables that appear to be the same. JsonConvert
will look at the first row of data to determine the column datatype, which may be guessed incorrectly.
To get around this;
DataTable
and DataColumn
definitions in separate response objects.DataColumn
definitions in the response before reading in the table.DataTable
ignoring the schema defined by Json.It sounds like a lot, but its only three additional lines of code.
// Get our Column definitions and serialize them using an anoymous function.
var columns = dt.Columns.Cast().Select(c => new { DataPropertyName = c.ColumnName, DataPropertyType = c.DataType.ToString()});
resp.ObjSchema = JsonConvert.SerializeObject(columns);
resp.Obj = JsonConvert.SerializeObject(dt);
resp.ObjSchema
becomes;
[
{
"DataPropertyName": "RowId",
"DataPropertyType ": "System.Int32"
},
{
"DataPropertyName": "ItemName",
"DataPropertyType ": "System.String"
}
]
Instead of letting Json define the column definitions via dt = JsonConvert.DeserializeObject
we can use LINQ on our resp.ObjSchema
to define them ourselves. We'll use MissingSchemaAction.Ignore
to ignore the schema provided by Json.
// If your environment does not support dynamic you'll need to create a class for with DataPropertyName and DataPropertyType.
JsonConvert.DeserializeObject>(response.ObjSchema).ForEach(prop =>
{
dt.Columns.Add(new DataColumn() { ColumnName = prop.DataPropertyName, DataType = Type.GetType(prop.DataPropertyType.ToString()) });
});
// Merge the results ignoring the JSON schema.
dt.Merge(JsonConvert.DeserializeObject(response.Obj), true, MissingSchemaAction.Ignore);