2011-09-18 40 views
0

我對這個問題感到厭惡。我有一些表格在特定日期向某個地區發佈了競爭表。 我正在製作一個MVC應用程序,它會顯示一個按日期增長的圖表。LINQ增長圖

我試圖做一個方法,接收int天數並計算每天發佈的內容的數量直到當天。例如:

地區的en-US

  • 內容X - 日期12/03/2010
  • 含量y - 日期12/03/2010
  • 含量z - 日期13/03/2010

地區PT-BR

  • 內容A - 日期13/03/2010
  • 內容B - 日期14/03/2010

區域Ja-JP

  • 內容的J - 日期15/03/2010

所以圖表必須顯示:

日期12/03/2010:

  • EN-US:2
  • PT-BR:0
  • JA-JP:0

日期13/03/2010

  • EN-US:1
  • PT-BR:1
  • JA-JP:0

日期14/03/2010

  • EN-US:0
  • PT-BR:1
  • JA-JP:0

日期15/03/2010

  • EN-US:0
  • PT-BR:0
  • JA-JP:1

我感到無能爲力試圖使這個查詢。

任何人都可以幫忙嗎?

在這裏,其他的方法,我創建了每個國家展現一天按天數:

public KeyValuePair<string, List<KeyValuePair<string, int>>>[] ChartDayByDay(int days, string locale) 
    { 
     KeyValuePair<string, List<KeyValuePair<string, int>>>[] contagens; 

     DateTime inicio = DateTime.Today.AddDays(days * (-1)); 

     var qry = from c in db.XBLRegionalInfos 
        where c.PublishDate != null 
        && c.PublishDate > inicio 
        select c; 

     if (!String.IsNullOrEmpty(locale)) 
      qry = qry.Where(x => x.RegionId == locale); 

     var qry2 = (from c in qry 
        group c by c.RegionId into g 
        let count = g.Count() 
        where count > 0 
        select g).ToList(); 

     var regioes = db.XBLRegions.ToList(); 

     contagens = new KeyValuePair<string, List<KeyValuePair<string, int>>>[qry2.Count]; 

     for (int i = 0; i < qry2.Count; i++) 
     { 
      for (int j = 0; j < qry2[i].Count(); j++) 
      { 
       var pais = qry2[i].ElementAt(j).Region.Country; 

       contagens[i] = new KeyValuePair<string, List<KeyValuePair<string, int>>>(
        pais, CountPeriod(days, qry2[i].ElementAt(j).RegionId)); 
      } 
     } 

     return contagens; 
    } 
+0

不是很清楚你正試圖做或者你的數據是如何構建的。你能否詳細解釋一下?您是否在嘗試改進粘貼的代碼 - 它是否滿足您的需求?你的問題不清楚。 –

回答

2

這適用於LINQ到對象至少包括:

public KeyValuePair<string, List<KeyValuePair<string, int>>>[] ChartDayByDay(int days, string locale) 
{ 
    return db 
     .XBLRegionalInfos 
     .Where(x => string.IsNullOrEmpty(locale) || x.RegionId == locale) 
     .Where(x => x.PublishDate > DateTime.Today.AddDays(days * (-1))) 
     .GroupBy(x => new {x.PublishDate.Date, x.RegionId}) 
     .Select(x => new {x.Key, Count = x.Count()}) 
     .GroupBy(x => x.Key.Date) 
     .Select(x => 
       new KeyValuePair<string, List<KeyValuePair<string, int>>>(
        x.Key.ToString(), 
        x.Select(y => new KeyValuePair<string, int>(y.Key.RegionId, y.Count)).ToList())) 
     .ToArray(); 
}