二段Linq Groupby操作
var messages = list.GroupBy(p=>p.RefOrderNo,(k,v)=> new {OrderNo = k,SkuInfo = v}) .Select(p => { var item = p.SkuInfo.FirstOrDefault(); return new ReplaceLabelSkuMessage { OrderNo = p.OrderNo, Operation = new WMS.Message.Operation { Operator = item.CreateBy, OperateTime = item.CreateOn }, ReplaceLabelSkuDetail = p.SkuInfo.Select(s => new ReplaceLabelSkuDetail { SKU = s.SKU, Weight = s.Weight, Lenght = s.Lenght, Width = s.Width, Height = s.Height }).ToList() }; });
var request = deliveryList.GroupBy(g => g.DeliveryNo) .Select(delivery => { var item = delivery.FirstOrDefault(); var yewuRequest = new DeliveryTicketShipOutMessage { DeliveryTicketNo = item.DeliveryNo, DeliveryType = TMS.Message.DeliveryTicket.DeliveryType.Logistics, WarehouseCode = item.YewuWarehouseCode, ShipOutWarehouse = item.YewuWarehouseCode, SkuContainers = delivery.GroupBy(g => g.ContainerCode).Select(container => { var containerLine = container.FirstOrDefault(); return new LogisticsDeliveryShipOutCustomerAsnContainerRequest { ContainerId = containerLine.ContainerCode, customerOrderType = TMS.Message.Common.CutomerOrderType.First, TargetWarehouseCode = containerLine.TargetWarehouseCode, CustomerAsnId = containerLine.CustomerAsnId, PalletId = containerLine.PalletId, Weight = new Weight { Value = containerLine.Weight, Unit = WeightUnit.G }, Size = new Packing() { Length = containerLine.Length, Width = containerLine.Width, Height = containerLine.Height, Unit = LengthUnit.CM }, Lines = container.GroupBy(l => l.SKU).Select(line => { return new LogisticsDeliveryShipOutCustomerAsnContainerLineRequest { SKU = line.Key, Quantity = line.Sum(s => s.PackingQty), AccountNo = line.FirstOrDefault().CustomerCode }; }).ToList(), Bags = container.Where(p => p.BagCode.Length > 0).GroupBy(g => g.BagCode).Select(bag => { var bagid = bag.Key; return new LogisticsDeliveryShipOutCustomerAsnContainerBagRequest { BagId = bagid, Lines = bag.Select(bagline => new LogisticsDeliveryShipOutCustomerAsnContainerBagLineRequest { SKU = bagline.SKU, Quantity = bagline.PackingQty, AccountNo = bag.FirstOrDefault().CustomerCode }).ToList() }; }).ToList() }; }).ToList(), OperateBy = item.CreateBy, OperateOn = item.CreateOn }; return yewuRequest; }).ToList();