2013-06-21 4 views
2

내 컬렉션에 데이터를 집계하는 데 도움이 필요합니다.Linq Groupby 중첩 된 관찰 가능 컬렉션 합계

RrvResponse 클래스

/// <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; } 
    } 

구현

,

var responses = new ObservableCollection<RrvResponse>(); 

// ....Load responses... 
// ... 
// ... 

응답의 수가 5, 그래서 응답 내부 BoDPoints 5 ObservableCollection에 있습니다.

BOD points are, 

    /// <summary> 
     /// The bid offer point. 
     /// </summary> 
     public class BidOfferPoint 
     { 

      /// <summary> 
      /// Gets or sets the date. 
      /// </summary> 
      public DateTime Date { get; set; } 

      /// <summary> 
      /// Gets or sets the time. 
      /// </summary> 
      public string Time { get; set; } 

      /// <summary> 
      /// Gets or sets the volume. 
      /// </summary> 
      public decimal Volume { get; set; } 

      /// <summary> 
      /// Gets or sets the price. 
      /// </summary> 
      public decimal Price { get; set; } 
     } 

샘플,

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 

관찰 가능한 컬렉션 이사회 - 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 

관찰 가능한 컬렉션 이사회 - 3

2013-06-15 
00:00 
100 
10 

2013-06-15 
00:15 
120 
15 

2013-06-15 
00:30 
150 
9 

그때 시간에 걸쳐 날짜별로 그룹 싶습니다 컬렉션 볼륨을 집계하십시오. 따라서 위의 예에서 21:00 ~ 20:00에 대한 모든 볼륨을 집계해야하며 00:15에 대한 21-06-2013의 모든 볼륨을 집계해야합니다.

Linq를 사용하는 가장 좋은 방법은 무엇입니까?

답변

1

당신은 항목과 이후 그룹을 집계 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) 
        }) 
     }); 

다음과 같은 루프를 사용할 수 있습니다 (예를 들어, 출력에 총 볼륨)

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); 
    } 
} 
+0

을 볼륨 중? –

+0

아, 총 볼륨을 원한다. 편집 된 답변을 참조하십시오. –

+0

고마워, 매력처럼 작동! –