[英]C# Datareader to DTO?
DATA 数据
Name | Value | Date |
cat1 | 3 | date1 |
cat1 | 5 | date2 |
cat1 | 2 | date5 |
cat2 | 6 | date8 |
cat2 | 7 | date1 |
cat2 | 2 | date6 |
DTO DTO
public class MeterReadingsChartData
{
public string name { get; set; }
public List<DateTime> dates { get; set; }
public List<double> values { get; set; }
}
Converting 转换
// 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();
After these three conversation, my data looks like this: 在这三个对话之后,我的数据如下所示:
cat1: { values: {3 ,5, 2}, dates: {date1, date2, data5}
cat2: { values: {6 ,7, 2}, dates: {date8, date1, data6}
Can I directly conversation from db data to DTO. 我可以直接从数据库数据到DTO进行对话吗? For example, I want somethig like following. 例如,我想要类似下面的内容。 (there may be another data access to db) (可能有其他对db的数据访问)
IEnumerable<MeterReadingsChartData> MeterReadingsChartData;
while (reader.Read())
{
// fill chart data DTO
}
Is it possible and How? 有可能吗?如何? I hope, I can explain... 希望我能解释...
Given that your dates
and values
properties are mutable lists, you could just read a row at a time (ungrouped), and key a dictionary keyed by name, adding dates and values as you go: 鉴于您的dates
和values
属性是可变列表,您可以一次读取一行(未分组),然后键入以名称为关键字的字典,并随即添加日期和值:
var readings = new Dictionary<string, MeterReadingsChartData>();
while (reader.Read())
{
string name = reader.GetString(0);
DateTime date = reader.GetDateTime(1);
double value = reader.GetDouble(2);
MeterReadingsChartData group;
if (!readings.TryGetValue(name, out group))
{
group = new MeterReadingsChartData {
Name = name,
Values = new List<double>(),
Dates = new List<DateTime>()
};
readings[name] = group;
}
group.Values.Add(value);
group.Dates.Add(date);
}
A few notes: 一些注意事项:
ToArray
, which wouldn't actually compile if your properties are really lists... if your properties are actually arrays, the approach above wouldn't work 在您的LINQ代码中,您使用了ToArray
,如果您的属性是真正的列表,那么它实际上不会编译...如果您的属性是真正的数组,那么上述方法将不起作用 List<Tuple<DateTime, double>>
or even create a separate Reading
class with just a date and a value. 您可能要使用单个List<Tuple<DateTime, double>>
,甚至创建一个仅带有日期和值的单独的Reading
类,而不是使用两个单独的列表。 AddReading(DateTime, double)
method to make the calling code cleaner. 您可以为DTO提供一个接受名称的构造函数,在构造函数中初始化列表,使列表属性为只读,还可以创建AddReading(DateTime, double)
方法以使调用代码更AddReading(DateTime, double)
。 @JonSkeet, I try to do your suggestion like following, and it works. @JonSkeet,我尝试按照以下步骤提出您的建议,并且它可以工作。 But I dont know following code are fully same your suggestion... 但是我不知道以下代码与您的建议完全相同...
DTO DTO
public class MeterReadingsChartData
{
public string Name { get; set; }
public string TypeName { get; set; }
public string Unit { get; set; }
public List<Tuple<DateTime, double>> DateAndValue { get; set; }
public MeterReadingsChartData(string name, List<Tuple<DateTime, double>> dateAndValue, string typeName, string unit)
{
this.Name = name;
this.DateAndValue = dateAndValue;
this.TypeName = typeName;
this.Unit = unit;
}
}
data fetching and converting 数据获取和转换
var readings = new Dictionary<string, MeterReadingsChartData>();
while (reader.Read())
{
string name = reader.GetString(5);
DateTime date = reader.GetDateTime(1);
double value = reader.GetDouble(2);
string typeName = reader.GetString(3);
string unit = reader.GetString(4);
MeterReadingsChartData group;
if (!readings.TryGetValue(name, out group))
{
group = new MeterReadingsChartData(name, new List<Tuple<DateTime, double>>(), typeName, unit);
readings[name] = group;
readings[typeName] = group;
readings[unit] = group;
}
group.DateAndValue.Add(new Tuple<DateTime, double>(date, value));
}
result is my expected. 结果是我的预期。
Thanks a lot. 非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.