簡體   English   中英

在此Linq GroupBy查詢上需要幫助

[英]Need help on this Linq GroupBy query

我正在生成一份報告,其中將顯示一段時間內的每日使用情況。

我有活動和日子,但是我希望獲得有關實現以下目標的指導:

  • 天的分組(因此應存在12/02/2018的單個條目)
  • 活動列匯總當天的條目(例如,2018年12月2日為35)
  • 代表RecordCount的新列(這是每天發現的條目數量,例如12/02/2018將顯示2。)

到目前為止我有什么

// Engagement - Daily activity over time
PageStatistics.Where(x => x.Status == 1)
    .GroupBy(x => new { Day = x.CreatedAt.Date, x.AppID })
    .Select(x => new { Day = x.Key.Day, Activity = x.Count() })
    .OrderByDescending(x => x.Day)
    .ToList()
    .Select(x => new { Day = x.Day.ToString("dd/MM/yyyy"), Activity = x.Activity })

非常感謝。

更新

我正在尋找日,活動(所有AppID的總數)和RecordCount(不同的AppID條目)。

在此處輸入圖片說明

在我看來,您已經在做自己想要的事情。

特定

public class Page
{
   public DateTime CreatedAt { get; set; }
   public int Status { get; set; }
   public int AppID { get; set; }
}

CreatedAtAppId分組

var result = pageStatistics.Where(x => x.Status == 1)
           .GroupBy(
              x => new
                 {
                    Day = x.CreatedAt.Date,
                    x.AppID
                 })
           .OrderByDescending(x => x.Key.Day)
           .Select(
              x => new
                 {
                    Day = x.Key.Day.ToString("dd/MM/yyyy"),
                    AppID = x.Key.AppID,
                    RecordCount = x.Count()
                 })
           .ToList();

即您會獲得不同日期的所有應用程序活動的列表

注意 :我只是把AppID = x.Key.AppID ,以顯示您的組


但是,如果CreatedAt (與AppId無關)

var result = pageStatistics.Where(x => x.Status == 1)
           .GroupBy(x => x.CreatedAt.Date)
           .OrderByDescending(x => x.Key.Date)
           .Select(
              x => new
                 {
                    Day = x.Key.Date.ToString("dd/MM/yyyy"),
                    RecordCount = x.Count()
                 })
           .ToList();

更新

這些是您要尋找的Driods

var result = pageStatistics.Where(x => x.Status == 1)
              .GroupBy(
                    x => new
                       {
                          CreatedAt = x.CreatedAt.Date,
                          x.AppID
                       })
              .OrderByDescending(x => x.Key.CreatedAt)
              .Select(
                    x => new
                       {
                          x.Key.CreatedAt,
                          Activity = x.Count()
                       })
              .GroupBy(x => x.CreatedAt)
              .Select(
                    x => new
                    {
                       Day = x.Key.ToString("dd/MM/yyyy"),
                       Activity = x.Sum(y => y.Activity),
                       RecordCount = x.Count()
                    })
              .ToList();

以我的理解,以下內容就足夠了:

PageStatistics.Where(x => x.Status == 1)
    .GroupBy(x => new { Day = x.CreatedAt.Date })
    .Select(x => new { Date = x.Key.Date.ToString("dd/MM/yyyy"), 
                       Activity = x.Sum(y => y.AppID), 
                       Record = x.Count()});

回顧問題,我創建了以下Linqpad代碼,它們產生了預期的結果。 我沒有附加OrderBy或額外的Select ,您可以根據需要處理數據。 因為求和和計數的計算需要AppId,所以仍保留點數,因此我們不在分組邏輯中使用它,而對和(活動)和記錄(計數)值使用值投影。 還要注意,在Select語句投影中是IGrouping集合,可以使用Linq SelectSelectMany對其進行進一步處理。

void Main()
{
    List<Page> PageStatistics = new List<UserQuery.Page>();

    PageStatistics.Add(new Page(new DateTime(2018,02,12),3));
    PageStatistics.Add(new Page(new DateTime(2018,02,12),32));
    PageStatistics.Add(new Page(new DateTime(2018,02,11),20));
    PageStatistics.Add(new Page(new DateTime(2018,02,11),44));
    PageStatistics.Add(new Page(new DateTime(2018,02,11),20));
    PageStatistics.Add(new Page(new DateTime(2018,02,11),2));
    PageStatistics.Add(new Page(new DateTime(2018,02,11),1));
    PageStatistics.Add(new Page(new DateTime(2018,02,10),22));
    PageStatistics.Add(new Page(new DateTime(2018,02,10),2));
    PageStatistics.Add(new Page(new DateTime(2018,02,10),20));
    PageStatistics.Add(new Page(new DateTime(2018,02,10),10));


    var result  = 
    PageStatistics.Where(x => x.Status == 1)
    .GroupBy(x => new { Day = x.CreatedAt.Date })
    .Select(x => new { Date = x.Key.Date.ToString("dd/MM/yyyy"), 
                       Activity = x.Sum(y => y.AppID), 
                       Record = x.Count()});

    result.Dump();
}

public class Page
{
    public DateTime CreatedAt { get; set; }
    public int Status { get; set; }
    public int AppID { get; set; }

    public Page(DateTime c, int a)
    {
        CreatedAt = c;
        AppID = a;  
        Status = 1;
    }
}

結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM