c# - Select multiple fields group by and sum -
i want query linq (list of objects) , don't know how it, can group , sum can't select rest of fields. example:
id value name category 1 5 name1 category1 1 7 name1 category1 2 1 name2 category2 3 6 name3 category3 3 2 name3 category3 i want group id, sum value , return fields this.
id value name category 1 12 name1 category1 2 1 name2 category2 3 8 name3 category3
updated : if you're trying avoid grouping fields, can group id:
data.groupby(d => d.id) .select( g => new { key = g.key, value = g.sum(s => s.value), name = g.first().name, category = g.first().category }); but code assumes each id, same name , category apply. if so, should consider normalizing @aron suggests. imply keeping id , value in 1 class , moving name, category (and whichever other fields same same id) class, while having id reference. normalization process reduces data redundancy , dependency.
Comments
Post a Comment