Reputation: 1977
I'm trying to convert Datatable columns into array of Series (which I will later use to serialize them to Json), but not sure how to convert array of string to IList? Or maybe I should use other approach? but not sure how to convert array of string to IList<SeriesValue>
? Or maybe I should use other approach?
public Series[] GetGraphData(string sp)
{
var connection = ConfigurationManager.ConnectionStrings["EFDbContext"].ConnectionString;
using (var da = new SqlDataAdapter("exec " + sp, connection))
{
var dt = new DataTable();
da.Fill(dt);
da.FillSchema(dt, SchemaType.Mapped);
Series[] arrSeries = new Series[dt.Columns.Count];
foreach(DataColumn dc in dt.Columns)
{
if (dc.Ordinal == 0)
{
}
else
{
Series s = new Series()
{
seriesname = dc.ColumnName,
renderas = "Line",
data = dt.Rows.Cast<DataRow>().Select(row => row[dc.Ordinal]).Cast<SeriesValue>().ToList()
};
arrSeries[dc.Ordinal] = s;
}
}
return arrSeries;
}
}
public class Series
{
public string seriesname { get; set; }
public string renderas { get; set; }
public IList<SeriesValue> data { get; set; }
}
public class SeriesValue
{
public string value { get; set; }
}
Json format example:
{
"seriesname": "Input",
"renderas": "Line",
"data": [
{
"value": "6500"
},
{
"value": "6500"
},
{
"value": "6500"
}
}
Any help is much appreciated?
Upvotes: 2
Views: 3064
Reputation: 169
You can transform strings into objects like this:
given: strarr = array of strings
data = strarr.Select(o => new SeriesValue { value = o }).ToList()
now data is a List<SeriesValue>
, which implements IList<SeriesValue>
.
Upvotes: 6