C# 具有复杂结果的c-Linq
我有以下课程:C# 具有复杂结果的c-Linq,c#,linq,mapreduce,group-by,ravendb,C#,Linq,Mapreduce,Group By,Ravendb,我有以下课程: class Voucher { string AccountId { get; set; } Date Date { get; set; } // This class only implements the date part decimal Amount { get; set; } } 对以下数据进行简化/分类,以便澄清: 1 12/02/2014 100 1 12/02/2014 100 1 23/11/2014 100 1 23/11/2014
class Voucher
{
string AccountId { get; set; }
Date Date { get; set; } // This class only implements the date part
decimal Amount { get; set; }
}
对以下数据进行简化/分类,以便澄清:
1 12/02/2014 100
1 12/02/2014 100
1 23/11/2014 100
1 23/11/2014 100
2 10/01/2014 100
2 10/01/2014 100
2 09/08/2014 100
我希望结果按帐户和日期分组,但金额应为小于日期键的所有条目的总和:
1 12/02/2014 200
1 23/11/2014 400
2 10/01/2014 200
2 09/08/2014 300
我想不出一个解决办法,我可以做到这一点。以日期为关键字分组将给出该特定日期的总金额,我不需要这些金额。这能做到吗
此外,我需要在一个查询中完成此操作,因为我将使用此查询进行RavenDB reduce。编辑:使用多组密钥更新
我可以这样做
输出:
var result = from i in input
group i by new { i.Date.Date, i.AccountId }
into grouped
select new {
accountId = grouped.Key.AccountId,
date = grouped.Key.Date,
total = (from kv in input
where kv.Date.Date <= grouped.Key.Date && kv.AccountId == grouped.Key.AccountId
select kv).Sum(i => i.Amount)
};
我想应该是2015年1月10日而不是2014年,是吗。。?否则,2014年1月10日也低于2014年2月12日,可能需要明确说明您也在按AccountId分组。至少我是这么认为的,因为一切都小于或等于2014年11月23日,而你的数学只有在你也限制在AccountId内时才有意义。为什么2014年2月12日的总数只有200?2014年1月10日少于2002年12月/2014@Chris,我认为结果很明显。我的错误。编辑。谢谢你指出这一点。
var input = new[] {
new Vocuher() {
AccountId = "1",
Date = DateTime.Parse("12/02/2014"),
Amount = 100
},
new Vocuher() {
AccountId = "1",
Date = DateTime.Parse("12/02/2014"),
Amount = 100
},
new Vocuher() {
AccountId = "1",
Date = DateTime.Parse("23/11/2014"),
Amount = 100
},
new Vocuher() {
AccountId = "1",
Date = DateTime.Parse("23/11/2014"),
Amount = 100
},
new Vocuher() {
AccountId = "2",
Date = DateTime.Parse("10/01/2014"),
Amount = 100
},
new Vocuher() {
AccountId = "2",
Date = DateTime.Parse("10/01/2014"),
Amount = 100
},
new Vocuher() {
AccountId = "2",
Date = DateTime.Parse("09/08/2014"),
Amount = 100
}
};