.net - LINQ Group By and aggregate fields -
i've struggled grouping in linq, i'd group based on date part of datetime field , aggregate other fields.
example table layout
mydatefield myintfield1 myintfield2 myintfield3 01/02/2013 5 5 5 01/02/2013 5 5 5 02/02/2013 10 10 10 02/02/2013 10 10 10
i'd return list with
mydatefield myintfield1 myintfield2 myintfield3 01/02/2013 10 10 10 02/02/2013 20 20 20
i've managed following using msdn examples can't head around how use it. states should able access new bookedlist
group keep getting annonymous type errors , such.
dim bestdays = in b a.bookedon < a.eventdaytime , a.cancelled = false group bookdate = a.bookedon.value.date bookedlist = group
the syntax into [field_name1] = [aggregate_function1]([value1]), [field_name2] = [aggregate_function2]([value2]) ...
. key automatically part of returned object.
so looking this:
class tablerow public property mydatefield date public property myintfield1 integer public property myintfield2 integer public property myintfield3 integer sub new(mydate date, myint1 integer, myint2 integer, myint3 integer) mydatefield = mydate myintfield1 = myint1 myintfield2 = myint2 myintfield3 = myint3 end sub end class sub main() dim tablesrows new list(of tablerow) tablesrows.add(new tablerow(today, 5, 10, 15)) tablesrows.add(new tablerow(today, 6, 11, 16)) dim v = r in tablesrows group r.mydatefield total1 = sum(r.myintfield1), total2 = sum(r.myintfield2), total3 = sum(r.myintfield3) end sub
Comments
Post a Comment