I am having the following Data
List<Trades> tradeList = new List<Trades>();
tradeList.Add(new Trades() { securityId = "JKH.N0000", principalAmount = "100.00", tradeDate ="20160401", accountNumber = "JKB831230063VN00" });
tradeList.Add(new Trades() { securityId = "JKH.N0000", principalAmount = "200.50", tradeDate = "20160402", accountNumber = "JKB547779605VN00" });
tradeList.Add(new Trades() { securityId = "COMB.N0000", principalAmount = "150.00", tradeDate = "20160403", accountNumber = "JKB831230063VN00" });
tradeList.Add(new Trades() { securityId = "JKH.N0000", principalAmount = "100.00", tradeDate = "20160409", accountNumber = "JKB547779605VN00" });
tradeList.Add(new Trades() { securityId = "GRAN.N0000", principalAmount = "200.00", tradeDate = "20160409", accountNumber = "JKB813384150VN00" });
tradeList.Add(new Trades() { securityId = "COMB.N0000", principalAmount = "100.00", tradeDate = "20160410", accountNumber = "JKB813384150VN00" });
tradeList.Add(new Trades() { securityId = "HNB.X0000", principalAmount = "100.00", tradeDate = "20160410", accountNumber = "JKB000009903FC00" });
tradeList.Add(new Trades() { securityId = "HNB.N0000", principalAmount = "100.00", tradeDate = "20160410", accountNumber = "JKB000000054LC00" });
I am having a requirement to get the Sum of principalAmount
for given Age group. So I am visualizing the output as the following:
Age Group | Sum(principalAmount) 0-25 | v 25-35 | w 35-55 | x 55-75 | y 75 > | z
For such I did the following
var rx = new Regex("JKB([0-9]{9})(V|X)N([0-9]{2})+", RegexOptions.IgnoreCase);
var q = tradeList
.Where (x => rx.IsMatch(x.accountNumber))
.Select(r => new
{
accountNo = r.accountNumber,
dob = calculateDOB(r.accountNumber.Substring(3)),
Age = calculateAge(calculateDOB(r.accountNumber.Substring(3))),
Gender = int.Parse(r.accountNumber.Substring(3).Substring(2, 3)) > 500 ? "Female" : "Male",
revenue = r.principalAmount
});
Then Grouped the data as following
var ceilings = new[] { 0, 25, 35, 55, 75 };
var q2 = q.GroupBy(item => ceilings.First(ceiling => ceiling >= item.Age));
Dictionary<int, decimal> counts = q2.ToDictionary(g => g.Key, (g => g.Sum(x => decimal.Parse(x.revenue.ToString()))));
string s = string.Empty;
foreach (KeyValuePair<int, decimal> entry in counts)
{
s += s + string.Format("Key = {0}, Value = {1}", entry.Key, entry.Value) + Environment.NewLine;
}
MessageBox.Show(s);
But I am getting the data as output as:
Key = 35, Value = 550.00 Key = 75, Value = 300.50
My question is How can I get the Key value as a string in the format of the following:
Age Group | Sum(principalAmount) 0-25 | v 25-35 | w 35-55 | x 55-75 | y 75 > | z
I got direction on the following StackOverFlow Question and Answers