AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

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

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. For example, I want somethig like following. (there may be another data access to db)

IEnumerable<MeterReadingsChartData> MeterReadingsChartData;
while (reader.Read())
{
     // fill chart data DTO
}

Is it possible and How? I hope, I can explain...

Upvotes: 2

Views: 1642

Answers (2)

AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

@JonSkeet, I try to do your suggestion like following, and it works. But I dont know following code are fully same your suggestion...

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500495

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:

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:

  • I've PascalCased your property names to follow .NET naming conventions
  • In your LINQ code you've use ToArray, which wouldn't actually compile if your properties are really lists... if your properties are actually arrays, the approach above wouldn't work
  • Rather than have two separate lists, you may want to use a single List<Tuple<DateTime, double>> or even create a separate Reading class with just a date and a value.
  • You could give your DTO a constructor accepting the name, initialize the list in the constructor, make the list properties read-only, and also create an AddReading(DateTime, double) method to make the calling code cleaner.

Upvotes: 4

Related Questions