C# Linq Groupby Sum嵌套可观察集合
我需要一些帮助来收集我收集的数据 RrvResponse类C# Linq Groupby Sum嵌套可观察集合,c#,.net,linq,C#,.net,Linq,我需要一些帮助来收集我收集的数据 RrvResponse类 /// <summary> /// The RRV response. /// </summary> public class RrvResponse { /// <summary> /// Initializes a new instance of the <see cref="RrvResponse"/> class.
/// <summary>
/// The RRV response.
/// </summary>
public class RrvResponse
{
/// <summary>
/// Initializes a new instance of the <see cref="RrvResponse"/> class.
/// </summary>
public RrvResponse()
{
this.BoDPoints = new ObservableCollection<BidOfferPoint>();
}
/// <summary>
/// Gets or sets the id.
/// </summary>
public string Id { get; set; }
/// <summary>
/// Gets or sets the message date.
/// </summary>
public DateTime MessageDate { get; set; }
/// <summary>
/// Gets or sets the BOD points.
/// </summary>
public ObservableCollection<BidOfferPoint> BoDPoints { get; set; }
}
可观测收集Bod-2
2013-06-21
00:00
Observable Collection Bod - 1
2013-06-21
00:00
100
10
2013-06-21
00:15
120
15
2013-06-21
00:30
150
9
40
1
2013-06-21
00:15
10
0.5
2013-06-21
00:30
11
0.1
可观测收集Bod-3
2013-06-15
00:00
100
10
2013-06-15
00:15
120
15
2013-06-15
00:30
150
9
我希望按日期分组,然后按时间分组,并汇总卷。因此,在上述示例中,应对2013年6月21日00:00时的所有卷进行聚合,应对2013年6月21日00:15时的所有卷进行聚合
使用Linq实现这一点的最佳方法是什么 您可以使用
SelectMany
对项目进行聚合,然后对项目进行分组:
var result = responses
.SelectMany(r => r.BoDPoints)
.GroupBy(p => p.Date)
.Select(byDate =>
new
{
Date = byDate.Key,
EntriesByTime = byDate
.GroupBy(p => p.Time)
.Select(byTime =>
new
{
Time = byTime.Key,
TotalVolume = byTime.Sum(p => p.Volume)
})
});
您可以使用以下循环(例如,输出总体积)
如何获得卷的总和?
2013-06-21
00:00
Observable Collection Bod - 1
2013-06-21
00:00
100
10
2013-06-21
00:15
120
15
2013-06-21
00:30
150
9
40
1
2013-06-21
00:15
10
0.5
2013-06-21
00:30
11
0.1
2013-06-15
00:00
100
10
2013-06-15
00:15
120
15
2013-06-15
00:30
150
9
var result = responses
.SelectMany(r => r.BoDPoints)
.GroupBy(p => p.Date)
.Select(byDate =>
new
{
Date = byDate.Key,
EntriesByTime = byDate
.GroupBy(p => p.Time)
.Select(byTime =>
new
{
Time = byTime.Key,
TotalVolume = byTime.Sum(p => p.Volume)
})
});
foreach (var byDate in result)
{
Console.WriteLine("Entries for date " + byDate.Date);
foreach (var byTime in byDate.EntriesByTime)
{
Console.WriteLine("Total volume for time " + byTime.Time + ": " + byTime.TotalVolume);
}
}