With the following code how would I join items together.
var shippedItems = _orderService.GetOrderById(shipment.OrderId).Shipments
.Where(x => x.ShippedDateUtc != null && x.OrderId == shipment.OrderId)
.Select(x => x.ShipmentItems)
.ToList();
So there may be two or more shipments for the order id. Each shipment may contain identical items.
Need a nudge in the right direction
Have tried
var shippedItems = _orderService.GetOrderById(shipment.OrderId).Shipments
.Where(x => x.ShippedDateUtc != null && x.OrderId == shipment.OrderId)
.Select(x => x.ShipmentItems.Join(x.ShipmentItems
.Where(y => y.ShipmentId == shipment.Id)))
.ToList();
and
var shippedItems = _orderService.GetOrderById(shipment.OrderId).Shipments
.Where(x => x.ShippedDateUtc != null && x.OrderId == shipment.OrderId)
.Select(x => x.ShipmentItems.GroupBy(y => y.Id))
.ToList();
and
var shippedItems = _orderService.GetOrderById(shipment.OrderId).Shipments
.Where(x => x.ShippedDateUtc != null && x.OrderId == shipment.OrderId)
.Select(x => x.ShipmentItems.Distinct())
.ToList();
Here is the code I'm using for output the items:
foreach (var shipmentItem in shippedItems)
{
System.Diagnostics.Debug.WriteLine("item = " + shipmentItem);
System.Diagnostics.Debug.WriteLine("item = " + shipmentItem.OrderItemId);
}
The above output produces:
shippedItemsList count = 9 item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356077
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356078
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356079
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356077
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356078
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356079
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356077
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356079
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356080
So the excepted output should be:
shippedItemsList count = 4 item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356077
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356079
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356078
item = System.Data.Entity.DynamicProxies.ShipmentItem_18BEAAFA747B42988EC4CB25D967298CC6736AF528389FC98E81143F7D629631 item = 356080
The above output is from using var shippedItems = _orderService.GetOrderById(shipment.OrderId).Shipments.Where(x => x.ShippedDateUtc != null && x.OrderId == shipment.OrderId).SelectMany(x => x.ShipmentItems).ToList();
NOTE: shipment item is ICollection<ShipmentItem> Shipment.ShipmentItems
when I hover it in visual studio
UPDATED senario:
So let's say that the order has 3 items(a = 3, b= 5, c = 3), Now 1 shipment is sent with items(a = 1, b = 2, c = 0), Now a second shipment is sent with items(a = 1, b = 1, c = 1). I would like the quantity of items from both shipments. So I expect items(a = 2, b = 3, c = 1), I currently get items(a = 1, b = 2) + items(a = 1, b = 1, c = 1). So the list of shipment items I loop through are appearing more than once.
I thought maybe union but not sure how to put it together :/
UPDATE MY SOLUTION
I could not manage to accomplish this using lambda expression, so I had to do the following solution to get the result I wanted. I hope this helps others looking for workarounds.
//calculate quantity of total shipped items from each shipment List<ShipmentItem> alreadyShippedItemsList = new List<ShipmentItem>(); Dictionary<int, int> alreadyShippedItems = new Dictionary<int, int>(); var shippedItems = _orderService.GetOrderById(shipment.OrderId).Shipments .Where(x => x.ShippedDateUtc != null) .SelectMany(x => x.ShipmentItems) .ToList(); //create a list of shipment items for (int i = 1; i <= shipmentsList.Count - 1; i++) { var si = shipmentsList[i]; var sii = si.ShipmentItems.ToList(); foreach (var item in sii) { var itemInList = alreadyShippedItemsList.Where(x => x.OrderItemId == item.OrderItemId).FirstOrDefault(); int sum = 0; //create a list of shipment items and check for duplicate items if (itemInList == null) { alreadyShippedItems.Add(item.OrderItemId, item.Quantity); alreadyShippedItemsList.Add(item); } else { //if duplicate item is found update the quantity in the dictionary sum = itemInList.Quantity + item.Quantity; alreadyShippedItems[item.OrderItemId] = sum; } } }