I have a List of database data in which 2 properties are important to group on. I stripped out the non-important properties and created a List of the data in which it needs to be sorted.
Not sure if some method with algorithm with linq statement should be done, and then how would it be best to store the results?
public class PaymentVouchers
{
public string PayFromBankAccountId {get; set; }
public int PaymentType {get; set;}
public decimal TotalPaymentAmount {get; set;}
}
Data:
var paymentVouchers = new List<PaymentVouchers>()
{
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 5},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 5},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 5},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 6},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 6},
new PaymentVouchers { PayFromBankAccountId = "ABC", PaymentType = 6},
new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 5},
new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 5},
new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 6},
new PaymentVouchers { PayFromBankAccountId = "DEF", PaymentType = 6},
}
Thus from the data, I need to group ABC and 5 together as there are 3 of those , then group together ABC and 6 , there are also 3 of those by chance. Then in the data 2 other groups are seen DEF and 5 - 2 of those and DEF and 6 , there are 2 of those.
So if I did a foreach loop, a linq statement - How do I keep them from getting messy with grouping them into what type of collection ? Hashset of Tuples ? Some new List with a new property that is an identifier "key" . Performance is ideal
Thoughts?
Additions: update:
I'm having trouble putting that data in the list into a new list
var paymentGroups = new List<PaymentGroups>();
paymentGroups = paymentVouchers.GroupBy(x => new { x.PayFromBankAccountId, x.PaymentType })
.Select(x => new
{
x.Key.PayFromBankAccountId,
x.Key.PaymentType,
//x.Sum(PaymentType)
Sum = x.Sum(TotalPaymentAmount),
paymentGroups.TotalCount = x.Key.Count
});
paymentGroups.Dump();
}
public class PaymentGroups {
public string PayFromBankAccountId { get; set; }
public int PaymentType { get; set; }
public decimal TotalAmounts { get; set; }
public int TotalCount {get; set; }
}