/// <summary> /// Convert this definition to a data table so it can be populated /// </summary> /// <returns></returns> public DataTable ToDataTable() { DataTable result = new DataTable(); // Build a results table to send back // Loop the items in the definition and add them to the column definition ItemProperties.ForEach(property => { switch (property.PropertyType) { case DataItemPropertyType.Property: // Create the new column DataColumn newColumn = new DataColumn(property.Name, property.DataType) { AllowDBNull = true }; // Add the column to the array result.Columns.Add(newColumn); // If this is a key column, add it to the table primary keys list if (property.Key) { result.PrimaryKey = result.PrimaryKey.Append(newColumn).ToArray(); // Append to the end } break; case DataItemPropertyType.Calculated: try { // Try and add the column in result.Columns.Add( new DataColumn(property.Name, property.DataType, property.Calculation) { AllowDBNull = true }); } catch { #warning "Bit of a hack for now, expression columns can reference other expression columns but they need to be added in the right order, fix this later by adding the expressions after the fact" // Cannot create a calculated column based on another calculated column // So add in a column where the calculation is empty result.Columns.Add( new DataColumn(property.Name, property.DataType, "") { AllowDBNull = true }); } break; } }); return(result); // Return the data table }