c# - LINQ: how to aggregate list within object after grouping properties -
i have object chartobject contains list. have list of chartobjects used linq remove duplicate attributes. don't know how combine each"itemlist" property of chartobjects.
public class chartobject { public string type { get; set; } public int year { get; set; } public long cost { get; set; } public list<items> itemlist { get; set; } } list<chartobject> colist = getitems(); result = colist.groupby(i => new { i.type, i.year }).select(g => new chartobject { type = g.key.type, year = g.key.year, cost = g.sum(i => i.cost), itemlist = g.select(i => i.itemlist) //does not work }).tolist(); i'm assuming it's not simple casting itemlist compiler says , there must sort of aggregation done?
assuming want combine of items in group, you're looking selectmany, , you'll need call tolist list<items>:
g.selectmany(i => i.itemlist).tolist();
Comments
Post a Comment