C# Linq double groupby need min/max for hourly values once per day -
i have been playing around awhile , can't quite result looking for.
i have object this:
public class point { public string tag {get;set;} public datetime time {get;set;} public int value {get;set;} }
each tag have 24 values per day (one per hour). data (tag / time / value):
x / 5-15-2015 - 0100 / 10 x / 5-15-2015 - 0200 / 20 x / 5-15-2015 - 0300 / 30 y / 5-15-2015 - 0100 / 20 y / 5-15-2015 - 0200 / 30 x / 5-16-2015 - 0100 / 10
for example...
i sort tag , date, min/max/avg 24 hrs in each day. goal create following object.
public class newpoint { public string tag {get;set;} public datetime date {get;set;} public int lowvalue {get;set;} public int highvalue {get;set;} public int avgvalue {get;set;} }
where resulting objests (tag / date / lowvalue / highvalue / avgvalue):
x / 5-15-2015 / 10 / 30 / 20 y / 5-15-2015 / 20 / 30 / 25 x / 5-16-2015 / 10 / 10 / 10
i having issues with:
group list new {list.tag, list.time.tostring("d") } dategroup
i think going need 2 linq statements. 1 group tag , date find min/max/avg each tag/date, , grab date[0] (or distinct date per tag) along min/max/avg pass newpoint object.
advice welcome, thanks!
no need 2 statements @ all... nor using string conversion. use key of each group, , datetime.date
property:
var query = list.groupby(x => new { x.tag, x.time.date }) .select(g => new newpoint { tag = g.key.tag, date = g.key.date, lowvalue = g.min(x => x.value), highvalue = g.max(x => x.value), avgvalue = (int) g.average(x => x.value) });
the cast avgvalue
required because return type of average
double
- might want consider changing property type instead.
Comments
Post a Comment