sql 使用 northwind 数据库查询到 c#

sql query to c# using northwind DB

使用 northwind 数据库,我必须进行查询以获取员工姓名、每位员工的订单量以及这些订单的平均价格

这是 SQL

中的查询
SELECT TOP 10
a.LastName, a.FirstName, amountOfOrders = COUNT(DISTINCT b.OrderID), AveragePricePerOrder = SUM(c.Quantity*c.UnitPrice) /COUNT(DISTINCT b.OrderID)
FROM Employees a join orders b on (a.EmployeeID = b.EmployeeID)
    join [Order Details] c on b.OrderID = c.OrderID
Group BY a.EmployeeID, a.LastName, a.FirstName
ORDER BY amountOfOrders Desc

这运行良好,但我必须在 C# 中实现,但我有点卡住了

到目前为止,我得到了这个

var query_rx = (from c in ctx.Employees
                        join or in ctx.Orders on c.EmployeeID equals or.EmployeeID
                        join ord in ctx.Order_Details on or.OrderID equals ord.OrderID
                        group c by new 
                        { 
                             c.EmployeeID, 
                             c.LastName, 
                             c.FirstName, 
                             amount = c.Orders.Count 
                        } into c
                        orderby c.Key.amount descending
                        select new
                        {
                            c.Key.LastName,
                            c.Key.FirstName,
                            amountOfOrders = c.Key.amount
                        }).Take(10);

"edit" 我在计算平均值时遇到问题,尝试了很多方法但我无法让它工作

"edit" 在 Dohnal 的建议的帮助下,我对查询做了一些更改。 这看起来几乎完全像我想要的列, 除了 字段 lastname 和 firstname 是 blank,即使使用 ToString

var query_rx = (from or in ctx.Order_Details
                        join ord in ctx.Orders on or.OrderID equals ord.OrderID
                        group or by new
                        {
                            ord.EmployeeID
                        } into c
                        orderby c.Select(x => x.OrderID).Distinct().Count() descending
                        select new
                        {
                            Lastname = (from emp in ctx.Employees
                                       where c.Key.EmployeeID == emp.EmployeeID
                                       select emp.LastName),
                            Firstname = (from emp in ctx.Employees
                                        where c.Key.EmployeeID == emp.EmployeeID
                                        select emp.FirstName),
                            c.Key.EmployeeID,
                            AmountOfOrders = c.Select(x => x.OrderID).Distinct().Count(),
                            AveragePricePerOrder = c.Sum(x => x.Quantity * x.UnitPrice) / c.Select(x => x.OrderID).Distinct().Count()
                        }).Take(10);

试试这个查询:

var query = (from emp in ctx.Employers
             join order in ctx.Orders on emp.EmployeeID equals order.EmployerID 
             join orderDet in ctx.Order_Details on order.OrderID equals orderDet.OrderID 
             group new { emp, order, orderDet } 
             by new { emp.FirstName, emp.LastName, emp.EmployeeID,  order.OrderID } 
             into orderGroup
             let a = new
             {
                 orderGroup.Key.EmployeeID,
                 orderGroup.Key.FirstName,
                 orderGroup.Key.LastName,
                 orderGroup.Key.OrderID,
                 sum1 = orderGroup.Sum(x => x.orderDet.Quantity * x.orderDet.UnitPrice),
             }
             group a by new { a.FirstName, a.LastName, a.EmployeeID } into empGroup
             let a2 = new
             {
                 empGroup.Key.FirstName,
                 empGroup.Key.LastName,
                 sum = empGroup.Sum(x => x.sum1),
                 count = empGroup.Count()
             }
             orderby a2.count descending
             select new
             {
                 a2.FirstName,
                 a2.LastName,
                 amountOfOrders = a2.count,
                 AveragePricePerOrder = a2.sum / a2.count
             }).Take(10);