private static async Task<IEnumerable<HitlistInfo>> GetPositionHitlistAsync(IEnumerable<WorkMonth> months, int lookBackInWeeks, PositionSearchViewModel posSearchViewModel)
{
return await Task.Factory.StartNew(() => {
if (posSearchViewModel != null)
{
var allDays = months.SelectMany(m => m.Days);
var daysFromLookback = lookBackInWeeks > 0 ? allDays.Where(m => m.DateTime > DateTime.Now.AddDays(lookBackInWeeks * -7)) : allDays;
var hitlistInfos = daysFromLookback
.SelectMany(d => d.Items)
.GroupBy(p => p.Position)
.Select(g =>
new HitlistInfo(
g.Key,
g.Count(),
g.Sum(wi => wi.HoursDuration),
posSearchViewModel.GetDescriptionForPositionNumber(g.Key))
);
return hitlistInfos.OrderByDescending(g => g.HoursUsed);
}
else
{
return Enumerable.Empty<HitlistInfo>();
}
});
}