c# - LINQ to group a list and return a list of a custom objects which has an embedded Dictionary -
i have large dataset , trying group dataset set of fields. in dataset have rows(of balances) 5 consecutive dates. goal return list of custom object has idictionary field key date , balance value. tried like:
int[] dt = new int[] {20160725,20160726,20160727,20160728,20160729}; var tranformeddata = posdata .groupby(p => new {p.symbol, p.account}) .select(gp => new tposmodel { symbol = gp.key.symbol, account = gp.key.account, balances = new dictionary<int, decimal>{ { gp.where(gpi => gpi.busdate == dt[0]).select(gpi => gpi.busdate), gp.where(gpi => gpi.busdate == dt[0]).select(gpi => gpi.balance) }, { gp.where(gpi => gpi.busdate == dt[1]).select(gpi => gpi.busdate), gp.where(gpi => gpi.busdate == dt[1]).select(gpi => gpi.balance) }, . . . }
this code causing duplicate key error. got work converting dictionary list of tuple not desired final outcome. have suggestion on how accomplish please.
thanks
it looks need internal group on busdate
after filter out not in dt
, need aggregate balance
. use sum
have here or else first().balance
. depends on how want handle multiple balances on same date.
int[] dt = new int[] {20160725,20160726,20160727,20160728,20160729}; var tranformeddata = posdata .groupby(p => new {p.symbol, p.account}) .select(gp => new tposmodel { symbol = gp.key.symbol, account = gp.key.account, balances = gp.where(gpi => dt.contians(gpi.busdate)) .groupby(gpi => gpi.busdate) .todictionary(g => g.key, g => g.sum(x => x.balance) });
as side note might want store dates datetime
instead of int
.
Comments
Post a Comment