我很感激有关改进此linq查询的任何建议。我正在查询发票清单,目的是找到每月发票总额最高的客户。然后,我想显示年,月,客户和发票总额。
发票:
public class Invoice
{
public string Customer { get; set; }
public DateTime Date { get; set; }
public double Amount { get; set; }
}
用于创建发票列表的数据上下文:
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;
}
}
}
查询:
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/>");
}
}
感谢您的建议。
答案 0 :(得分:2)
怎么样:
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);
}
一条建议:使用OrderBy
+ First
代替First
+ Max
来查找具有max属性值的项目,可能更好