score:3

Accepted answer
DateTime last60Days = new DateTime(xxx,xxx,xxx);

var query = from u in context.Users
            group u by new
            {
              u.Forename,
              u.Surname
            } into ug
            select new
            {
              Forename = ug.Key.Forename,
              Surname = ug.Key.Surname,
              MaxOrderdate = ug.SelectMany(x => x.Orders).Max(x => x.OrderDate),
              OrderCount = ug.SelectMany(x => x.Orders).Count(),
              Payment = ug.SelectMany(x => x.Orders.SelectMany(y => y.Payments)).Sum(x => x.PaymentAmount),
              OrderCountLast60 = ug.SelectMany(x => x.Orders).Where(x => x.OrderDate > last60Days).Count(),
              PaymentLast60 = ug.SelectMany(x => x.Orders.Where(x => x.OrderDate > last60Days).SelectMany(y => y.Payments)).Sum(x => x.PaymentAmount)
            };

Related Articles