我想找到哪个员工拥有最多的订单:此代码带来了所有用户的所有订单数:
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()
};
}
}
我该怎么做?
最佳答案
尝试使用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);
关于c# - 返回查询中的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14921382/