.net - Returning indexed property from anonymous type -
my linq query returns totals each month of year, i'm exposing twelve properties (named january, february, march...) of anonymous type. makes impossible loop through twelve values. there way instead return single indexed property containing 12 values?
this have:
dim mytotals = aggregate costrow in alltargetrows _ january = sum(costrow.january), february = sum(costrow.february), march = sum(costrow.march), _ ... total = sum(costrow.total)
this want achieve (compiler doesn't one):
dim mytotals = aggregate costrow in alltargetrows _ values = {sum(costrow.january), sum(costrow.february), sum(costrow.march), _ ... }, total = sum(costrow.total)
i able mytotals.values(i)
kind of stuff.
the first thing pops mind me use aggregate
function populate dictionary. this:
dim mytotals = alltargetrows .aggregate( new dictionary<string, int>() { { "january", 0 }, { "february", 0 }, { "march", 0 }, { "april", 0 }, { "may", 0 }, { "june", 0 }, { "july", 0 }, { "august", 0 }, { "september", 0 }, { "october", 0 }, { "november", 0 }, { "december", 0 }, { "total", 0 } }, (acc, costrow) => { acc["january"] += costrow.january; acc["february"] += costrow.february; acc["march"] += costrow.march; acc["april"] += costrow.april; acc["may"] += costrow.may; acc["june"] += costrow.june; acc["july"] += costrow.july; acc["august"] += costrow.august; acc["september"] += costrow.september; acc["october"] += costrow.october; acc["november"] += costrow.november; acc["december"] += costrow.december; acc["total"] += costrow.total; return acc; });
this result in dictionary keys months of year (and 'total') , values sums of values each month.
sorry, missed vb.net tag. vb.net not strong suit think equivalent in vb.net.
dim mytotals = _ alltargetrows _ .aggregate( _ new dictionary(of string, integer) { _ {"january", 0}, _ {"february", 0}, _ {"march", 0}, _ {"april", 0}, _ {"may", 0}, _ {"june", 0}, _ {"july", 0}, _ {"august", 0}, _ {"september", 0}, _ {"october", 0}, _ {"november", 0}, _ {"december", 0}, _ {"total", 0} _ }, _ function(acc, costrow) acc("january") += costrow.january acc("february") += costrow.february acc("march") += costrow.march acc("april") += costrow.april acc("may") += costrow.may acc("june") += costrow.june acc("july") += costrow.july acc("august") += costrow.august acc("september") += costrow.september acc("october") += costrow.october acc("november") += costrow.november acc("december") += costrow.december acc("total") += costrow.total return acc end function)
i not know if overload of aggregate
function can converted query syntax. (possibly simpler) alternative forego linq altogether , create dictionary before running query , loop on each row , populate dictionary way. this:
dim mytotals = _ new dictionary(of string, integer) { _ {"january", 0}, _ {"february", 0}, _ {"march", 0}, _ {"april", 0}, _ {"may", 0}, _ {"june", 0}, _ {"july", 0}, _ {"august", 0}, _ {"september", 0}, _ {"october", 0}, _ {"november", 0}, _ {"december", 0}, _ {"total", 0}} each costrow alltargetrow in alltargetrows mytotals("january") += costrow.january mytotals("february") += costrow.february mytotals("march") += costrow.march mytotals("april") += costrow.april mytotals("may") += costrow.may mytotals("june") += costrow.june mytotals("july") += costrow.july mytotals("august") += costrow.august mytotals("september") += costrow.september mytotals("october") += costrow.october mytotals("november") += costrow.november mytotals("december") += costrow.december mytotals("total") += costrow.total next
Comments
Post a Comment