public static DataTable ConvertPrimaryDataToDatatable(DatasetVersion dsv, IEnumerable<AbstractTuple> dsVersionTuples)
{
DataTable dt = new DataTable();
dt.TableName = "Primary data table";
DataStructureManager dsm = new DataStructureManager();
StructuredDataStructure sds = dsm.StructuredDataStructureRepo.Get(dsv.Dataset.DataStructure.Id);
XmlDocument doc = new XmlDocument();
doc = (XmlDocument)sds.Extra;
if (dsVersionTuples != null && sds != null && doc != null)
{
IEnumerable<XElement> orderList = XmlUtility.GetXElementByNodeName("variable", XmlUtility.ToXDocument(doc));
foreach (XElement element in orderList)
{
var vu = sds.Variables.Where(v => v.Id.Equals(Convert.ToInt64(element.Value))).FirstOrDefault();
// use vu.Label or vu.DataAttribute.Name
DataColumn col = dt.Columns.Add("ID"+vu.Id.ToString()); // or DisplayName also
col.Caption = vu.Label;
switch (vu.DataAttribute.DataType.SystemType)
{
case "String":
{
col.DataType = Type.GetType("System.String");
break;
}
case "Double":
{
col.DataType = Type.GetType("System.Double");
break;
}
case "Int16":
{
col.DataType = Type.GetType("System.Int16");
break;
}
case "Int32":
{
col.DataType = Type.GetType("System.Int32");
break;
}
case "Int64":
{
col.DataType = Type.GetType("System.Int64");
break;
}
case "Decimal":
{
col.DataType = Type.GetType("System.Decimal");
break;
}
case "DateTime":
{
col.DataType = Type.GetType("System.DateTime");
break;
}
default:
{
col.DataType = Type.GetType("System.String");
break;
}
}
if(vu.Parameters.Count>0)
{
foreach (var pu in vu.Parameters)
{
DataColumn col2 = dt.Columns.Add(pu.Label.Replace(" ", "")); // or DisplayName also
col2.Caption = pu.Label;
}
}
}
foreach (var tuple in dsVersionTuples)
{
dt.Rows.Add(ConvertTupleIntoDataRow(dt,tuple, sds));
}
}
return dt;
}