C#: Trying to aggregate a dictionary with a tuple<datetime,int> key using the datetime and one of the associated values -


i've spent few hours trying piece having hard time finding example. have dictionary tuple key , set of values class. want able aggregate (by averaging) value sets datetime , stored value aggregate indicator.

i struggling how aggregate based on single part of tuple key , stored value. if in sql like:

select datetime, [aggregate value], avg(myaggregatedfields)  dictionary  group datetime, [aggregate value] 

but tuple tripping me on one. appreciated, if pointing toward tutorial. reason used tuple needed have 2 values ensure uniqueness.

maybe this:

    class myclass     {         public datetime time { get; set; }         public int otherval { get; set; }         public double aggfield { get; set; }     }             list<myclass> lst = new list<myclass>();          lst.add(new myclass { time = datetime.today, otherval = 4, aggfield = 1});         lst.add(new myclass { time = datetime.today, otherval = 4, aggfield = 2 });         lst.add(new myclass { time = datetime.today, otherval = 3, aggfield = 3 });          var grouped = lst.groupby(c => new tuple<datetime, int>(c.time, c.otherval));         var dic = new dictionary<tuple<datetime, int>, double>();          foreach (var group in grouped)         {             var agg = group.average(c => c.aggfield);             dic.add(group.key, agg);         } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -