I would appreciate any suggestions on improving this linq query. I am querying a list of invoices, with the objective of finding the customer with the highest invoice total for each month. I then want to display the year, month, customer and invoice total.
The invoice:
public class Invoice
{
public string Customer { get; set; }
public DateTime Date { get; set; }
public double Amount { get; set; }
}
The data context to create a list of invoices:
public class DataContext
{
private List<Invoice> _invoices;
private List<string> _customers;
public List<Invoice> Invoices
{
get
{
if (_invoices == null)
{
_customers = new List<string>(){ "Jim", "John", "Jeff", "Joe", "Jack"};
_invoices = new List<Invoice>();
Random random = new Random();
for (int i = 0; i < 1000; i++)
{
_invoices.Add(new Invoice() {
Customer = _customers[random.Next(0, 5)],
Date = new DateTime(random.Next(2010, 2015), random.Next(1, 13), random.Next(1, 20)),
Amount = random.Next(1,1000)
});
}
}
return _invoices;
}
}
}
The query:
DataContext context = new DataContext();
var invoiceTotalsByMonth = from invoice in context.Invoices
group invoice by invoice.Date.Year into yearGroup
orderby yearGroup.Key
select new
{
Year = yearGroup.Key,
Months = from year in yearGroup
group year by year.Date.Month into monthGroup
orderby monthGroup.Key
select new
{
Month = monthGroup.Key,
CustomerTotals = from month in monthGroup
group month by month.Customer into customerGroup
select new
{
Customer = customerGroup.Key,
Total = customerGroup.Sum(i=>i.Amount)
}
}
};
foreach (var year in invoiceTotalsByMonth)
{
Response.Write(year.Year + "<br/>");
foreach (var month in year.Months)
{
var maxCustomer = month.CustomerTotals.First(i => i.Total == month.CustomerTotals.Max(j => j.Total));
Response.Write(month.Month + ": " + maxCustomer.Customer + " - " + maxCustomer.Total.ToString("c") + "<br/>");
}
}
Thank you for your suggestions.
How about that:
DataContext context = new DataContext();
var invoiceTotalsByMonthQuery = from i in context.Invoices
group i by new { i.Date.Year, i.Date.Month } into g
select new
{
Year = g.Key.Year,
Month = g.Key.Month,
Customer = g.GroupBy(x => x.Customer)
.Select(x => new { Name = x.Key, Total = x.Sum(y => y.Amount)})
.OrderByDescending(x => x.Total)
.First()
};
var invoiceTotalsByMonth = invoiceTotalsByMonthQuery.OrderBy(x => x.Year)
.ThenBy(x => x.Month);
foreach(var item in invoiceTotalsByMonth)
{
Console.WriteLine("{0}/{1} - {2} ({3})", item.Month, item.Year, item.Customer.Name, item.Customer.Total);
}
One advice: it's probably better to use OrderBy
+ First
instead of First
+ Max
to find item with the max property value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.