Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a three column table, I would like to group by the first and second columns and sum the third.

Is this possible in Linq? If so what is the syntax, I've found for a single column groupby but can't see how to expand it.
Posted

Thanks, I've got that working now:
C#
var query = feeAllRecords
    .GroupBy(f => new { f.AccountNo, f.FeeTypeID })
    .Select(group => new { fee = group.Key, total = group.Sum(f => f.FeeAmount) });


At the moment I'm then looping through this result set to convert to my final result set:
XML
List<inFee> feeRecords = new List<inFee>(feeAllRecords.Capacity);
foreach (var rec in query)
{
    inFee fee2 = new inFee();
    fee2.AccountNo = rec.fee.AccountNo;
    fee2.FeeTypeID = rec.fee.FeeTypeID;
    fee2.FeeAmount = rec.total;

    feeRecords.Add(fee2);
}


It works, but it seems 'incorrect', or at least that there should be a more efficent method??
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900