2

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

ekad
  • 14,436
  • 26
  • 44
  • 46
hiFI
  • 1,887
  • 3
  • 28
  • 57

1 Answers1

2

I'm assuming that 0 isn't really a ceiling value. Also assuming, as per your referenced post, that the list isn't fixed, so we need to solve generally.

Take the list of ceilings - we'll call it sourceCeilings. I'll assume that this will be an array as per your question and not a List:

var sourceCeilings = new int?[] { 25, 35, 55, 75 };

We want a set of 'floors' for your ceilings. Your question infers that "25" is the end of one range and the beginning of the next. So we'll go with that.

var floors = new List<int?> { 0 };
floors.AddRange(sourceCeilings);

Now add a null value to the end of ceilings - we'll need to convert to a List for this. The null is needed to pair off for "75 >":

var ceilings = sourceCeilings.ToList();
ceilings.Add(null);

Now we Zip the two lists together, creating a list of Floor, Ceiling and Title:

var combined = floors.Zip(ceilings,
    (f, c) =>
        new
        {
            floor = f,
            ceiling = c,
            title = string.Format("{0} > {1}", f.Value, c.HasValue ? c.Value.ToString() : "")
        });

With this List you can perform your join on the Ceiling and select the key as Title.

Brett
  • 1,540
  • 9
  • 13