Linq查询需要正确的和
本文关键字:查询 Linq | 更新日期: 2023-09-27 18:25:37
我正在使用.NET 4数据可视化图表,并使用Linq查询来获取图表的数据。我是林克的新手,所以不知道如何使用这个查询。这是它的作用,我有下面的表格,它从中提取数据
- 订单
- 发货时间表
订单表包含已订购的产品、OrderId、已发货的金额以及该订单的剩余数量。一个订单中只能有一个产品。我正在尝试获取类OrderShipment中的所有OrderShipment,制作一个List<>并按每个产品对其进行分组,并显示已订购的每个产品的总数量和已发货的产品。下面的查询汇总了所有订单的发货数量,这是正确的,但因为它是一个订单发货列表,我想要所有发货中每个订单的剩余数量,并按产品汇总。现在它将所有发货的剩余数量相加,这是错误的,因为订单在所有发货中都有相同的剩余数量。如何获得每个产品的剩余数量,以便查询正确地将每个订单的剩余数量相加??如果你有建议,请提供一个如何实现这一点的代码示例,你的帮助真的很感激,谢谢
private class ChartDataPoint
{
public string ProductName { get; set; }
public double QtyRemaining { get; set; }
public double QtyShipped { get; set; }
}
private class OrderShipment
{
public string ProductName { get; set; }
public double QuantityRemaining { get; set; }
public double QuantityShipped { get; set; }
public int OrderId { get; set; }
}
List<OrderShipment> productsOrdered =
(from os in Statistics.OptimalShipments
from o in Statistics.OrdersPlaced
where ((os.Date >= Statistics.ShippingWindowStartDate) &&
(os.Date <= Statistics.ShippingWindowEndDate) &&
(os.OrderId == o.OrderId) &&
((o.ClientLocationId == ClientLocation.ClientLocationId)))
select new OrderShipment()
{
ProductName = o.Product.Name,
QuantityRemaining = o.RemainingQuantity,
QuantityShipped = os.QuantityShipped,
}).ToList();
var query = productsOrdered.GroupBy(p => p.ProductName);
List<ChartDataPoint> chartDataPoints = new List<ChartDataPoint>();
foreach (var productGroup in query)
{
chartDataPoints.Add(new ChartDataPoint()
{
ProductName = productGroup.Key,
// This is obv wrong this sums up the Remaining quantity across
// all shipments for a order when we should be only taking the
//Remaining quantity once for each order across all shipments.
QtyRemaining = productGroup.Sum(po => po.QuantityRemaining),
QtyShipped = productGroup.Sum(po => po.QuantityShipped)
});
}
据我所知,您的productGroup应该有许多按产品名称分组的OrderShipment
对象。然后你想求和QuantityShipped和QuantityRemaining应该都是一样的,你想只取这个值吗?
如果是这样,那么这应该对你有用:
QtyRemaining = productGroup.First();
如果我误解了,你可能想简化解释,也许可以举个例子。。。