我正在制作一份报告,显示每日使用情况。
我有活动和日子,但是我想知道如何实现以下目标:
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 })
非常感谢。
更新
我正在寻找Day,Activity(所有AppID的总计数)和RecordCount(Distinct AppID条目)。
答案 0 :(得分:3)
在我看来,你已经在做你想做的事了。
给出
public class Page
{
public DateTime CreatedAt { get; set; }
public int Status { get; set; }
public int AppID { get; set; }
}
按CreatedAt
和AppId
分组
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();
答案 1 :(得分:1)
在我的理解中,以下就足够了:
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
,您可以根据需要按摩数据。由于SumI和Count计算需要AppId,因此我们不会在分组逻辑中使用它,并使用Sum(活动)和记录(计数)值的值投影。另请注意,在Select语句中,投影是一个IGrouping集合,可以使用Linq Select
进一步处理SelectMany
<强>代码强>
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;
}
}
结果: