Linq to Entities Join、Group、Sum with Northwind Orders
Linq to Entities Join, Group, Sum with Northwind Orders
我正在尝试编写一个 linq 查询,它应该对 Northwind.mdb 中的 order_detail 行求和,然后 return 汇总总数以及负责员工的一些详细信息。我正在使用 LINQpad 对其进行测试,这是我目前所拥有的
void Main()
{
var result = (from e in Employees
join o in Orders on e.EmployeeID equals o.EmployeeID
join d in OrderDetails on o.OrderID equals d.OrderID
where o.OrderID == 10250
group new { e, o, d } by new
{
o.OrderID, e.Address, e.BirthDate, e.City, e.Country,
e.LastName, e.FirstName, e.Title, e.TitleOfCourtesy, e.HireDate,
e.Region, e.PostalCode, e.HomePhone,
e.Extension, e.ReportsTo,e.PhotoPath, e.EmployeeID,
d.Quantity, d.UnitPrice, d.Discount
}
into grp select new
{
Name = grp.Key.FirstName + " " + grp.Key.LastName,
OrderID = grp.Key.OrderID,
Address = grp.Key.Address,
SalesTotal = grp.Sum(x => x.d.UnitPrice * x.d.Quantity)
});
result.Dump();
}
Output from LINQPad
我只期待一行,总计 1813.00。有人可以告诉我我做错了什么吗?
您没有在正确的级别分组。如果将其更改为按员工分组,那么您应该会得到正确的结果:
void Main()
{
var result = (from e in Employees
join o in Orders on e.EmployeeID equals o.EmployeeID
join d in OrderDetails on o.OrderID equals d.OrderID
where o.OrderID == 10250
group new { e, o, d } by new
{
e.EmployeeID, e.FirstName, e.LastName, e.Address
}
into grp select new
{
Name = grp.Key.FirstName + " " + grp.Key.LastName,
Address = grp.Key.Address,
SalesTotal = grp.Sum(x => x.d.UnitPrice * x.d.Quantity)
});
result.Dump();
}
如果您希望每个订单一行,也按此分组:
void Main()
{
var result = (from e in Employees
join o in Orders on e.EmployeeID equals o.EmployeeID
join d in OrderDetails on o.OrderID equals d.OrderID
where o.OrderID == 10250
group new { e, o, d } by new
{
e.EmployeeID, e.FirstName, e.LastName, e.Address,
o.OrderID
}
into grp select new
{
Name = grp.Key.FirstName + " " + grp.Key.LastName,
OrderID = grp.Key.OrderID,
Address = grp.Key.Address,
SalesTotal = grp.Sum(x => x.d.UnitPrice * x.d.Quantity)
});
result.Dump();
}
我正在尝试编写一个 linq 查询,它应该对 Northwind.mdb 中的 order_detail 行求和,然后 return 汇总总数以及负责员工的一些详细信息。我正在使用 LINQpad 对其进行测试,这是我目前所拥有的
void Main()
{
var result = (from e in Employees
join o in Orders on e.EmployeeID equals o.EmployeeID
join d in OrderDetails on o.OrderID equals d.OrderID
where o.OrderID == 10250
group new { e, o, d } by new
{
o.OrderID, e.Address, e.BirthDate, e.City, e.Country,
e.LastName, e.FirstName, e.Title, e.TitleOfCourtesy, e.HireDate,
e.Region, e.PostalCode, e.HomePhone,
e.Extension, e.ReportsTo,e.PhotoPath, e.EmployeeID,
d.Quantity, d.UnitPrice, d.Discount
}
into grp select new
{
Name = grp.Key.FirstName + " " + grp.Key.LastName,
OrderID = grp.Key.OrderID,
Address = grp.Key.Address,
SalesTotal = grp.Sum(x => x.d.UnitPrice * x.d.Quantity)
});
result.Dump();
}
Output from LINQPad
我只期待一行,总计 1813.00。有人可以告诉我我做错了什么吗?
您没有在正确的级别分组。如果将其更改为按员工分组,那么您应该会得到正确的结果:
void Main()
{
var result = (from e in Employees
join o in Orders on e.EmployeeID equals o.EmployeeID
join d in OrderDetails on o.OrderID equals d.OrderID
where o.OrderID == 10250
group new { e, o, d } by new
{
e.EmployeeID, e.FirstName, e.LastName, e.Address
}
into grp select new
{
Name = grp.Key.FirstName + " " + grp.Key.LastName,
Address = grp.Key.Address,
SalesTotal = grp.Sum(x => x.d.UnitPrice * x.d.Quantity)
});
result.Dump();
}
如果您希望每个订单一行,也按此分组:
void Main()
{
var result = (from e in Employees
join o in Orders on e.EmployeeID equals o.EmployeeID
join d in OrderDetails on o.OrderID equals d.OrderID
where o.OrderID == 10250
group new { e, o, d } by new
{
e.EmployeeID, e.FirstName, e.LastName, e.Address,
o.OrderID
}
into grp select new
{
Name = grp.Key.FirstName + " " + grp.Key.LastName,
OrderID = grp.Key.OrderID,
Address = grp.Key.Address,
SalesTotal = grp.Sum(x => x.d.UnitPrice * x.d.Quantity)
});
result.Dump();
}