IEnumerable <CheckPointSlice> Group(IEnumerable <Projections.BudgetLine> lines)
        {
            var sch = _checkPoints.OrderBy(d => d.Date)
                      .Select(s => new CheckPointLines {
                Id = s.Id, Date = s.Date, Name = "non c'è !"
            })
                      .Concat(new[] { new CheckPointLines {
                                          Date = DateTime.MaxValue
                                      } })
                      .ToArray();

            int index   = 0;
            var current = sch[index];

            foreach (var l in lines.OrderBy(d => d.Date))
            {
                if (l.Date < current.Date)
                {
                    current.Lines.Add(l);
                }
                else
                {
                    index++;
                    current = sch[index];
                    current.Lines.Add(l);
                }
            }

            var result = new List <CheckPointSlice>();

            for (int i = 0; i < sch.Length; i++)
            {
                var s        = sch[i];
                var fromDate = DateTime.MinValue;
                if (i != 0)
                {
                    fromDate = sch[i - 1].Date;
                }

                var slice = new CheckPointSlice
                {
                    Id       = s.Id,
                    Date     = s.Date,
                    FromDate = fromDate,
                    Name     = s.Name,
                    Groups   = s.Lines.GroupBy(d => d.DistributionKey)
                               .Select(c => new DistributionGroups(c.Key,
                                                                   c.GroupBy(r => r.Category).Select(u => new IdNameAmount {
                        Id = u.Key, Name = u.Key, Amount = u.Select(o => o.Amount).Sum()
                    })))
                               .OrderByDescending(d => d.Name)
                               .ToList(),
                    TotalAmount = s.Lines.Select(r => r.Amount).Sum(),
                };
                result.Add(slice);
            }
            return(result.OrderByDescending(r => r.Date).ToArray());
            //var grps = sch.Select(s => new CheckPointSlice
            //{
            //    Id = s.Id,
            //    Date = s.Date,
            //    Name = s.Name,
            //    Groups = s.Lines.GroupBy(d => d.DistributionKey)
            //        .Select(c => new DistributionGroups(c.Key,
            //                        c.GroupBy(r => r.Category).Select(u => new IdNameAmount { Id = u.Key, Name = u.Key, Amount = u.Select(o => o.Amount).Sum() })))
            //                        .OrderByDescending(d => d.Name)
            //                        .ToList(),
            //    TotalAmount = s.Lines.Select(r => r.Amount).Sum(),
            //});

            //return grps.OrderByDescending(d => d.Date).ToList();
        }
        IEnumerable<CheckPointSlice> Group(IEnumerable<Projections.BudgetLine> lines)
        {
            var sch = _checkPoints.OrderBy(d => d.Date)
                .Select(s => new CheckPointLines { Id = s.Id, Date = s.Date, Name = "non c'è !" })
                .Concat(new[] { new CheckPointLines { Date = DateTime.MaxValue } })
                .ToArray();

            int index = 0;
            var current = sch[index];

            foreach (var l in lines.OrderBy(d => d.Date))
            {
                if (l.Date < current.Date)
                {
                    current.Lines.Add(l);
                }
                else
                {
                    index++;
                    current = sch[index];
                    current.Lines.Add(l);
                }
            }

            var result = new List<CheckPointSlice>();
            for (int i = 0; i < sch.Length; i++)
            {
                var s = sch[i];
                var fromDate = DateTime.MinValue;
                if (i != 0)
                    fromDate = sch[i - 1].Date;

                var slice = new CheckPointSlice
                {
                    Id = s.Id,
                    Date = s.Date,
                    FromDate = fromDate,
                    Name = s.Name,
                    Groups = s.Lines.GroupBy(d => d.DistributionKey)
                        .Select(c => new DistributionGroups(c.Key,
                                        c.GroupBy(r => r.Category).Select(u => new IdNameAmount { Id = u.Key, Name = u.Key, Amount = u.Select(o => o.Amount).Sum() })))
                                        .OrderByDescending(d => d.Name)
                                        .ToList(),
                    TotalAmount = s.Lines.Select(r => r.Amount).Sum(),
                };
                result.Add(slice);
            }
            return result.OrderByDescending(r => r.Date).ToArray();
            //var grps = sch.Select(s => new CheckPointSlice
            //{
            //    Id = s.Id,
            //    Date = s.Date,
            //    Name = s.Name,
            //    Groups = s.Lines.GroupBy(d => d.DistributionKey)
            //        .Select(c => new DistributionGroups(c.Key,
            //                        c.GroupBy(r => r.Category).Select(u => new IdNameAmount { Id = u.Key, Name = u.Key, Amount = u.Select(o => o.Amount).Sum() })))
            //                        .OrderByDescending(d => d.Name)
            //                        .ToList(),
            //    TotalAmount = s.Lines.Select(r => r.Amount).Sum(),
            //});

            //return grps.OrderByDescending(d => d.Date).ToList();
        }