我目前有一个DataTable,它在monthly view中包含我需要的数据。我想要做的是将这些数据分组到季度视图中,分组在ActivationYear,ActivationQuarter,BranchYear和BranchQuarter上。
我需要来自AmountFinanced和CurrentBalance的最新(季度的最后一个完整月份)值。但是,我需要本季度每个月付款的总和。
以下是我每月的例子:
ActivationDate | CurrentBranchDate | AmountFinanced | Payments | CurrentBalance
1/1/16 | 1/30/16 | $10,000 | $100 | $9,800
1/1/16 | 2/29/16 | $10,000 | $0 | $9,900
1/1/16* | 3/31/16* | $10,000* | $2,000* | $7,800*
1/1/16* | 4/30/16* | $10,000* | $1,000* | $6,800*
2/1/16 | 2/29/16 | $5,000 | $0 | $5,200
2/1/16* | 3/31/16* | $5,000* | $500* | $4,700*
2/1/16* | 4/30/16* | $5,000* | $200* | $4,500*
以下是每季度分组时所需的结果(上面带*的行是我用于CurrentBalance的行):
ActivationY | ActivationQ | BranchY | BranchQ | AmountFinanced | Payments | CurrentBalance
2016 | 1 | 2016 | 1 | $15,000 | $2,500 | $12,500
2016 | 1 | 2016 | 2 | $15,000 | $1,200 | $11,300
我已经尝试循环遍历月表,但我似乎得到重复和不正确的金额(例如:添加所有金额的资金列给我40,000美元资金,当它应该只有15,000美元)。
以下是基于jdweng答案的更新代码:
var testResults = monthlyTable.AsEnumerable()
.GroupBy(x => (x.Field<DateTime>("ActivationDate").Month - 1) / 3)
.Select(x => new
{
ActivationYear = x.FirstOrDefault().Field<DateTime>("ActivationDate").Year,
ActivationQuarter = ((x.FirstOrDefault().Field<DateTime>("ActivationDate").Month - 1) / 3) + 1,
BranchYear = x.FirstOrDefault().Field<DateTime>("CurrentBranchDate").Year,
BranchQuarter = ((x.FirstOrDefault().Field<DateTime>("CurrentBranchDate").Month - 1) / 3) + 1,
AmountFinanced = x.OrderByDescending(y => y.Field<DateTime>("CurrentBranchDate")).Select(y => y.Field<decimal>("AmountFinanced")).FirstOrDefault(),
CurrentBalance = x.OrderByDescending(y => y.Field<DateTime>("CurrentBranchDate")).Select(y => y.Field<decimal>("CurrentBalance")).FirstOrDefault(),
Sum_of_Payments = x.Sum(y => y.Field<decimal?>("Payments"))
}).ToList();
我仍然收到错误的AmountFinanced和CurrentBalance。我是否也需要在CurrentBranchDate上进行分组?
@jdweng,这是我正在使用的数据:
dt.Rows.Add(new object[] { DateTime.Parse("11/01/2016"), DateTime.Parse("11/30/2017"), 57000, 0, 53639.4 });
dt.Rows.Add(new object[] { DateTime.Parse("01/01/2017"), DateTime.Parse("11/30/2017"), 52000, 0, 52000 });
dt.Rows.Add(new object[] { DateTime.Parse("03/01/2017"), DateTime.Parse("11/30/2017"), 28000, 0, 27160.82 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("04/30/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("05/31/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("06/30/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("06/30/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("07/31/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("08/31/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("09/30/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("07/31/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("08/31/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("09/30/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("10/31/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("11/30/2017"), 77200, -1947.7, 74616.01 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("10/31/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("11/30/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("08/01/2017"), DateTime.Parse("08/31/2017"), 20000, 0, 20000 });
dt.Rows.Add(new object[] { DateTime.Parse("08/01/2017"), DateTime.Parse("09/30/2017"), 20000, 0, 20000 });
dt.Rows.Add(new object[] { DateTime.Parse("08/01/2017"), DateTime.Parse("10/31/2017"), 20000, -1631.58, 18540.78 });
dt.Rows.Add(new object[] { DateTime.Parse("08/01/2017"), DateTime.Parse("11/30/2017"), 20000, 0, 18540.78 });
dt.Rows.Add(new object[] { DateTime.Parse("10/01/2017"), DateTime.Parse("10/31/2017"), 25000, -509.55, 24490.45 });
dt.Rows.Add(new object[] { DateTime.Parse("10/01/2017"), DateTime.Parse("11/30/2017"), 25000, 0, 24490.45 });
这是我正在调用的修改项目:
var quarterResults = monthlyTable.AsEnumerable()
//.GroupBy(x => (x.Field<DateTime>("ActivationDate").Month - 1) / 3)
//.GroupBy(x => new { ActivationMonth = (x.Field<DateTime>("ActivationDate").Month - 1) / 3, ActivationYear = x.Field<DateTime>("ActivationDate").Year })
.GroupBy(x => new { ActivationQuarter = (x.Field<DateTime>("ActivationDate").Month - 1) / 3, ActivationYear = x.Field<DateTime>("ActivationDate").Year, BranchQuarter = (x.Field<DateTime>("CurrentBranchDate").Month - 1) / 3, BranchYear = x.Field<DateTime>("CurrentBranchDate").Year })
.Select(x => new[]
{
x.FirstOrDefault().Field<DateTime>("ActivationDate").Year,
((x.FirstOrDefault().Field<DateTime>("ActivationDate").Month - 1) / 3) + 1,
x.FirstOrDefault().Field<DateTime>("CurrentBranchDate").Year,
((x.FirstOrDefault().Field<DateTime>("CurrentBranchDate").Month - 1) / 3) + 1,
x.OrderByDescending(y => y.Field<DateTime>("CurrentBranchDate")).Select(y => y.Field<decimal>("AmountFinanced")).FirstOrDefault(),
x.OrderByDescending(y => y.Field<DateTime>("CurrentBranchDate")).Select(y => y.Field<decimal>("CurrentBalance")).FirstOrDefault(),
x.OrderByDescending(y => y.Field<DateTime>("CurrentBranchDate")).Select(y => y.Field<decimal>
x.Sum(y => y.Field<decimal?>("Payments"))
}).ToArray();
如果你查看它为ActivationYear 2017,ActivationQuarter 2,BranchYear 2017,BranchQuarter 2生成的数据,它给我77,200作为资助金额,它应该给我175,650(77,200 + 98,450)。
答案 0 :(得分:0)
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication31
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("ActivationDate", typeof(DateTime));
dt.Columns.Add("CurrentBranchDate", typeof(DateTime));
dt.Columns.Add("AmountFinanced", typeof(decimal));
dt.Columns.Add("Payments", typeof(decimal));
dt.Columns.Add("CurrentBalance", typeof(decimal));
dt.Rows.Add(new object[] { DateTime.Parse("11/01/2016"), DateTime.Parse("11/30/2017"), 57000, 0, 53639.4 });
dt.Rows.Add(new object[] { DateTime.Parse("01/01/2017"), DateTime.Parse("11/30/2017"), 52000, 0, 52000 });
dt.Rows.Add(new object[] { DateTime.Parse("03/01/2017"), DateTime.Parse("11/30/2017"), 28000, 0, 27160.82 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("04/30/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("05/31/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("06/30/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("06/30/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("07/31/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("08/31/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("09/30/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("07/31/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("08/31/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("09/30/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("10/31/2017"), 77200, 0, 76190.96 });
dt.Rows.Add(new object[] { DateTime.Parse("04/01/2017"), DateTime.Parse("11/30/2017"), 77200, -1947.7, 74616.01 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("10/31/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("06/01/2017"), DateTime.Parse("11/30/2017"), 98450, 0, 98450 });
dt.Rows.Add(new object[] { DateTime.Parse("08/01/2017"), DateTime.Parse("08/31/2017"), 20000, 0, 20000 });
dt.Rows.Add(new object[] { DateTime.Parse("08/01/2017"), DateTime.Parse("09/30/2017"), 20000, 0, 20000 });
dt.Rows.Add(new object[] { DateTime.Parse("08/01/2017"), DateTime.Parse("10/31/2017"), 20000, -1631.58, 18540.78 });
dt.Rows.Add(new object[] { DateTime.Parse("08/01/2017"), DateTime.Parse("11/30/2017"), 20000, 0, 18540.78 });
dt.Rows.Add(new object[] { DateTime.Parse("10/01/2017"), DateTime.Parse("10/31/2017"), 25000, -509.55, 24490.45 });
dt.Rows.Add(new object[] { DateTime.Parse("10/01/2017"), DateTime.Parse("11/30/2017"), 25000, 0, 24490.45 });
var quarterResults = dt.AsEnumerable()
.GroupBy(x => new { ActivationQuarter = (x.Field<DateTime>("ActivationDate").Month - 1) / 3, ActivationYear = x.Field<DateTime>("ActivationDate").Year })
.Select(x => new {
activationYear = x.FirstOrDefault().Field<DateTime>("ActivationDate").Year,
quarterResults = ((x.FirstOrDefault().Field<DateTime>("ActivationDate").Month - 1) / 3) + 1,
branchYear = x.FirstOrDefault().Field<DateTime>("CurrentBranchDate").Year,
branchQuarter = ((x.FirstOrDefault().Field<DateTime>("CurrentBranchDate").Month - 1) / 3) + 1,
AmountFinanced = x.GroupBy(y => new { ActivationDate = y.Field<DateTime>("ActivationDate")}).Select(z => z.OrderByDescending(a => a.Field<DateTime>("CurrentBranchDate")).Select(y => y.Field<decimal>("AmountFinanced")).FirstOrDefault()).Sum(),
CurrentBalance = x.OrderByDescending(y => y.Field<DateTime>("CurrentBranchDate")).Select(y => y.Field<decimal>("CurrentBalance")).FirstOrDefault(),
sum = x.Select(y => y.Field<decimal?>("Payments")).Sum()
}).ToArray();
}
}
}