繁体   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