2

我想找出哪个员工的订单最多:这段代码带来了所有用户的所有订单数:

    public void GetBestEmployeeFromDates(DateTime fromDate, DateTime toDate)
    {
        using (NorthwindDataContext db = new NorthwindDataContext())
        {
            var query =
                from z in db.Employees
                select new
                {
                    OrderNumber = z.Orders.Where(x => x.OrderDate > fromDate.Date).Count()
                };

        }

    }

我该怎么做?

4

2 回答 2

3

尝试使用MaxByhttp://code.google.com/p/morelinq/):

var maxOrders = query.MaxBy(o => o.OrderNumbers);

您还可以调整查询以包含客户名称:

var query =
            from z in db.Employees
            select new
            {   
                Customer = z.CustomerName,
                OrderNumber = z.Orders.Where(x => x.OrderDate > fromDate.Date).Count()
            };

var person = query.OrderByDescending(o => o.OrderNumber).Select(c=>c.Customer).First();

Console.WriteLine("The customer with the most orders is: " + person);
于 2013-02-17T13:08:19.260 回答
2

我在想这样的事情:

using (NorthwindDataContext db = new NorthwindDataContext())
{
 var query =
   (from z in db.Employees
   select new
   {
      OrderNumber = z.Orders.Where(x => x.OrderDate > fromDate.Date).Count(),
      Employee = z
   }).OrderByDescending(x => OrderNumber).Select(x => x.Employee).FirstOrDefault()
}
于 2013-02-17T13:18:40.050 回答