从LINQ获取范围组结果



我有以下数据

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

我的问题是如何获得以下格式的字符串键值:

Age Group |   Sum(principalAmount)
0-25      |   v       
25-35     |   w
35-55     |   x
55-75     |   y
75 >      |   z

我得到了以下StackOverFlow问答

的指导

我假设0不是一个真正的上限值。同样假设,根据你引用的帖子,这个列表不是固定的,所以我们需要普遍解决。

以天花板列表为例,我们称之为sourceCeilings。我假设这将是一个数组,而不是一个列表:

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

我们想要一套天花板的"地板"。你的问题推断出"25"是一个范围的结束和下一个的开始。所以我们就这么做吧。

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

现在,在天花板的末端添加一个null值——我们需要为此转换为List。需要为"75>"配对空值:

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() : "")
        });

使用此列表,您可以在天花板上执行连接,并选择关键字作为"标题"。

相关内容

  • 没有找到相关文章

最新更新