Code (CSharp): var s = datas.GroupBy(d => d.a).OrderByDescending(g => g.Sum(d => d.Dmg)) .Select(ud => string.Join(":", ud.Key, ud.Sum(d => d.Dmg).NumString())); dmmText.text = string.Join("\n", s); It's a temporary solution for damage meter, to show damage of units ranking. It works, but uses twice of Sum() function. Is one time Sum() possible? edit: question solved. see #3 if you want to know the solution.
If this is called every frame you really should not use LINQ for it, at all. If it's just every so often and you don't mind adding to the GC then maybe just store the results of first sum on a separate line.
Code (CSharp): var s = datas.GroupBy(d => d.a).Select(ud => new { ud.Key, total = ud.Sum(d => d.Dmg) }) .OrderByDescending(ud => ud.total).Select(ud => string.Join(":", ud.Key, ud.total.NumString())); It's now ok. The first time I tried Select() first but missing "Total=" for the Sum() then cause grammer error.