我有以下数据
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" });
我要求获得给定年龄组的principalAmount
之和。所以我将输出可视化如下:
Age Group | Sum(principalAmount) 0-25 | v 25-35 | w 35-55 | x 55-75 | y 75 > | z
为此,我做了以下
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
});
然后将数据分组如下
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);
但我将数据作为输出获取:
Key = 35, Value = 550.00 Key = 75, Value = 300.50
我的问题是如何将Key值作为字符串获取,格式如下:
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
答案 0 :(得分:2)
我假设0并不是真正的上限值。同样假设,根据你的推荐帖子,列表没有修复,所以我们需要一般解决。
列出天花板列表 - 我们将其称为sourceCeilings
。我假设这将是一个根据您的问题而不是列表的数组:
var sourceCeilings = new int?[] { 25, 35, 55, 75 };
我们想要一套&#39;楼层&#39;为你的天花板。你的问题推断出&#34; 25&#34;是一个范围的结束和下一个范围的开始。所以我们将继续这样做。
var floors = new List<int?> { 0 };
floors.AddRange(sourceCeilings);
现在在天花板的末尾添加一个空值 - 我们需要为此转换为List。需要为null配对&#34; 75&gt;&#34;:
var ceilings = sourceCeilings.ToList();
ceilings.Add(null);
现在我们将两个列表压缩在一起,创建一个Floor,Ceiling和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() : "")
});
使用此列表,您可以在天花板上执行连接,并选择键作为标题。