将最近2周的数据拆分为当前和前一周 [英] Split last 2 weeks of data into current and previous week

查看:126
本文介绍了将最近2周的数据拆分为当前和前一周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

趋势数据是使用当前UTC日期作为结束日期和当前UTC日期的开始日期-13(数据库将日期存储为UTC)的EventLog列表

  DateTime dUtcEnd = DateTime.UtcNow.Date; 
DateTime dUtcStart = dUtcEnd.AddDays(-13);

var trendData =(来自db.EventLogs中的e
其中e.EventDateTime> = dUtcStart& e.EventDateTime< = dUtcEnd
&& e .EventType =='some event'
select e).ToList();

有没有办法<跳过以周为单位递增,以填充当前和过去几周的数组值?

不知何故,我认为 Take Skip 需要基于EventDateTime来确定当前和过去的星期被认为是。

  var currentTrend = trendData.Take(7).Reverse(); 
var previousTrend = trendData.Skip(7).Take(7).Reverse();






为了进一步了解什么是试图完成,下面是一个Highcharts数组将如何填充的想法。



返回的结果将包含3个数组。

  WeeklyTrend results = new WeeklyTrend(); 
results.Categories =新字符串[7];
results.ThisWeek = new int [7];
results.LastWeek = new int [7];

使用今日价值将当前每周减少一天的类别填充。

  int dayCounter = 6; 
for(int i = 0; i <7; i ++)
{
results.Categories [i] = dUtcEnd.AddDays(-dayCounter).ToShortDateString();
dayCounter--;

查看Categories数组以查找EventDateTime匹配的数组值

  foreach(var cTrend in currentTrend)
{
var dayOfWeek = Array .FindIndex(results.Categories,c => c.Contains(cTrend.EventDateTime.ToShortDateString()));

results.ThisWeek [dayOfWeek] ++;

查看Categories数组以查找EventDateTime匹配的数组值数组中添加日期以匹配当前星期的日期。

  foreach(var pTrend in previousTrend)
{
var dayOfWeek = Array.FindIndex(results.Categories,c => c.Contains(pTrend.EventDateTime.AddDays(6).ToShortDateString()));

results.LastWeek [dayOfWeek] ++;
}

最终目标是填充Highcharts趋势图,显示来自当前的数据与前一周相比。

解决方案

我的意图是在你的linq中使用 GroupBy 。考虑到这一点,我做了一个快速谷歌,并提出了一些在这里。引起我注意的是这一件:



基本上,以解释它:

  DateTime dUtcEnd = DateTime.UtcNow.Date; 
DateTime dUtcStart = dUtcEnd.AddDays(-13);

var eventLogs = new List< EventLogs>();
$ b $ var weekTrendGroups = eventLogs
.Where(x => x.EventDateTime> = dUtcStart
&& x.EventDateTime< = dUtcEnd
& amp ;& x.EventType ==some event)
.Select(p => new
{
Event = p,
Year = p.EventDateTime.Year,
Week = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear
(p.EventDateTime,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday)
})
.GroupBy(x => new {x 。年,x.Week})
。选择((g,i)=>新
{
WeekGroup = g,
WeekNum = i + 1,
年= g.Key.Year,
CalendarWeek = g.Key.Week
});

foreach(weekTrendGroups中的var eventLogGroup)
{
Console.WriteLine(Week+ eventLogGroup.WeekNum);
foreach(eventLogGroup.WeekGroup中的var proj)
Console.WriteLine({0} {1} {2},
proj.Event.EventType,
proj.Event .EventDateTime.ToString(d),
proj.Event.Id);
}

- 固定功能数据属性。



所以,这里的数据在 DayOfWeek.Monday 的边界上按星期整齐排列。案件。你当然可以改变以适应。然后,您可以在> WeekNum 以及其中的组上进行派对。



这看起来像一个整洁的解决方案,因为您当然可以将您自己的谓词应用于它以根据需要进行分割。


The trendData is a list of EventLogs using the current UTC date as the end date and a start date of the current UTC date -13 (the db stores dates as UTC)

DateTime dUtcEnd = DateTime.UtcNow.Date;
DateTime dUtcStart = dUtcEnd.AddDays(-13);

var trendData = (from e in db.EventLogs
                where e.EventDateTime >= dUtcStart && e.EventDateTime <= dUtcEnd
                      && e.EventType == 'some event'
                select e).ToList();

Is there a way to Take and Skip in week increments in order to populate array values for the current and previous weeks?

Something along these lines? Somehow I imagine that the Take and Skip need to base it on the EventDateTime in order to determine what current and previous weeks are considered.

var currentTrend = trendData.Take(7).Reverse();
var previousTrend = trendData.Skip(7).Take(7).Reverse();


In order to give some further context into what is trying to be accomplished, below is an idea of how the Highcharts arrays are going to be populated.

The results returned would contain 3 arrays.

WeeklyTrend results = new WeeklyTrend();
results.Categories = new string[7];
results.ThisWeek = new int[7];
results.LastWeek = new int[7];

Populate the Categories with the Today value decremented by one day each time for the current week.

int dayCounter = 6;
for (int i = 0; i < 7; i++)
{
    results.Categories[i] = dUtcEnd.AddDays(-dayCounter).ToShortDateString();
    dayCounter--;
}

Look in the Categories array in order to find the array value where the EventDateTime matches the date in the array.

foreach (var cTrend in currentTrend)
{
    var dayOfWeek = Array.FindIndex(results.Categories, c => c.Contains(cTrend.EventDateTime.ToShortDateString()));

    results.ThisWeek[dayOfWeek]++;
}

Look in the Categories array in order to find the array value where the EventDateTime matches the date in the array with days added to match the current week..

foreach (var pTrend in previousTrend)
{
    var dayOfWeek = Array.FindIndex(results.Categories, c => c.Contains(pTrend.EventDateTime.AddDays(6).ToShortDateString()));

    results.LastWeek[dayOfWeek]++;
}

The end goal is to populate a Highcharts trend chart that shows data from the current week compared to the previous week.

解决方案

my inclination is to use a GroupBy inside your linq. with this in mind, I did a quick google and came up with a few here on SO. One that caught my eye was this one:

C# Linq or Lambda expression Group by Week IEnumerable list with date field

basically, to paraphrase it:

DateTime dUtcEnd = DateTime.UtcNow.Date;
DateTime dUtcStart = dUtcEnd.AddDays(-13);

var eventLogs = new List<EventLogs>();

var weekTrendGroups = eventLogs
    .Where(x => x.EventDateTime >= dUtcStart 
                && x.EventDateTime <= dUtcEnd
                && x.EventType == "some event")
    .Select(p => new
    {
        Event = p,
        Year = p.EventDateTime.Year,
        Week = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear
                (p.EventDateTime, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
    })
    .GroupBy(x => new { x.Year, x.Week })
    .Select((g, i) => new
    {
        WeekGroup = g,
        WeekNum = i + 1,
        Year = g.Key.Year,
        CalendarWeek = g.Key.Week
    });

foreach (var eventLogGroup in weekTrendGroups)
{
    Console.WriteLine("Week " + eventLogGroup.WeekNum);
    foreach (var proj in eventLogGroup.WeekGroup)
        Console.WriteLine("{0} {1} {2}",
            proj.Event.EventType,
            proj.Event.EventDateTime.ToString("d"),
            proj.Event.Id);
}

[edit] - fixed function to match your data attributes.

so, here the data is neatly chopped by week on the boundary of DayOfWeek.Monday in this case. You can of course change to suit. You'd then be able to take the WeekNum and party on the groups within that.

This looks like a neat solution as you can then of course apply your own predicates to it to slice it as you see fit.

这篇关于将最近2周的数据拆分为当前和前一周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆