Last Updated: September 29, 2021
·
509
· mwgriffith

Grouping over multiple fields in linq

Here's some code that I keep forgetting from time to time. :-) Here's the basic code for grouping over multiple fields in linq.

var fullGroupedUpdateBilling = (from ub in fullUpdateBilling
               group ub by new { ub.Id, ub.ctranscode, ub.ctransdesc} into g 
               select new UBUpdateBillingReportItem
               {
                     Id = g.Key.Id,
                     ctranscode = g.Key.ctranscode,
                     ctransdesc = g.Key.ctransdesc,
                     usage = g.Sum(t => t.usage),
                     amt = g.Sum(t => t.bbamt), 
                     bbamt = g.Sum(t => t.amt),
                     hasbb = hasBB                  
              });