Linq-to-sql超时问题

时间:2016-05-19 23:13:51

标签: c# asp.net performance linq linq-to-sql

我是linq查询和.net的学生和新手,所以我确信代码构造得非常糟糕。

我一直在获取一组linq查询的超时错误,如下例所示。我用它来生成报告。有人可以提供有关如何优化此查询的建议吗?任何建议都非常感谢。

public IList<CountryOrderReport> CountryGetListEndRange(DateTime startDate, DateTime endDate)
{
    var countryOrders = (from order in db.Orders

                         where order.FinancialStatus != "draft" && order.CreatedAt >= startDate && order.CreatedAt <= endDate

                         group order by order.Customer.BillingAddress.Country into users
                         select new CountryOrderReport
                         {

                             CountryName = users.Key,
                             TotalOrders = users.Select(g => g.OrderId).Distinct().Count(),

                             TotalSpent = users.Select(g => g.TotalDue).Distinct().Sum(),
                             AverageOrderValue = users.Select(g => g.TotalDue).Distinct().Sum() / users.Select(g => g.OrderId).Distinct().Count()

                         }).OrderByDescending(g => g.TotalSpent).Take(5).ToList();

    return countryOrders;
}

0 个答案:

没有答案