c# - 返回查询中的最大值

标签 c# .net sql linq

我想找到哪个员工拥有最多的订单:此代码带来了所有用户的所有订单数:

    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/

相关文章:

c# - 无法在 MVC 操作中返回 CSV 文件。 Not 无法访问已关闭的 Streamable

C# 将 char 转换为大于 256 的 int

c# - 更改文件名

c# - 如何在 .NET 图表的 Y 轴上对数据进行分组?

c# - 使用 .NET 以编程方式将第二个进程附加到 VS2010

sql - 如何选择小于时间戳的日期和时间?

mysql - 如何从 ListView 导出数据并保存在数据库中

用于音乐编程的 .NET API?

.net - WCF 配置 - 将其从 app.config 中分离出来

sql - 只返回具有空字段的行?