数据
Name | Value | Date |
cat1 | 3 | date1 |
cat1 | 5 | date2 |
cat1 | 2 | date5 |
cat2 | 6 | date8 |
cat2 | 7 | date1 |
cat2 | 2 | date6 |
DTO
public class MeterReadingsChartData
{
public string name { get; set; }
public List<DateTime> dates { get; set; }
public List<double> values { get; set; }
}
转换
// first, I fetching datas from db to dataTable
// second, I convert to datatable to another object list
// for example IEnumerable<Readings> readings
// third, like following; one more conversation too
var chartSeries = readings.GroupBy(x => new { x.Name })
.Select(g => new
{
name = g.Key.Name ,
values = g.Select(x => x.Value).ToArray(),
dates = g.Select(x => x.Date).ToArray()
}).ToArray();
经过这三个对话,我的数据如下所示:
cat1: { values: {3 ,5, 2}, dates: {date1, date2, data5}
cat2: { values: {6 ,7, 2}, dates: {date8, date1, data6}
我可以直接从数据库数据到 DTO 对话吗?例如,我想要像下面这样的东西。(可能有另一个对 db 的数据访问)
IEnumerable<MeterReadingsChartData> MeterReadingsChartData;
while (reader.Read())
{
// fill chart data DTO
}
有可能吗?如何?我希望,我可以解释...