我想找到哪个员工的订单最多:此代码会显示所有用户的所有订单数量:
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()
};
}
}
我应该怎么做?
答案 0 :(得分:3)
尝试使用MaxBy
(http://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);
答案 1 :(得分: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()
}