日期:2014-05-17  浏览次数:20424 次

List<T> 重复记录合并 数量相加
实体类如下
C# code

public partial class ProductItem
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string orderId { get; set; }
        /// <summary>
        /// 条形码
        /// </summary>
        public string barCode { get; set; }
        /// <summary>
        /// 产品名称
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 单价
        /// </summary>
        public string Price { get; set; }
        /// <summary>
        /// 订货数量
        /// </summary>
        public string quantity { get; set; }
        /// <summary>
        /// 规格
        /// </summary>
        public string Spec { get; set; }
        /// <summary>
        /// 优惠价
        /// </summary>
        public double preferPrice { get; set; }
        /// <summary>
        /// 原价
        /// </summary>
        public double costPrice { get; set; }
        /// <summary>
        /// 审核备注
        /// </summary>
        public string auditMemo { get; set; }

    }


需要在这里判断如果barCode 条形码相同的情况下合并为一个商品quantity 订货数量相加
C# code

List<Model.ProductItem> listProductItem = new List<Model.ProductItem>();
List<Model.SellOrderCart> listOrderCart = SellOrderCart.GetModelList(OrderID);
foreach (Model.SellOrderCart item in listOrderCart)
{
                        Model.ProductItem modelProductItem = new Model.ProductItem();
                        modelProductItem.orderId = item.OrderID.ToString();
                        modelProductItem.barCode = SellOrder.GetSpec(item.Title, item.ItemCode, item.GoodsTypeID, item.GoodsID, item.Specs, "", item.YGD);
                        modelProductItem.Title = SellOrder.GetTitle(item.Title, item.SetTableID);
                        modelProductItem.Price = item.SalePrice.ToString();
                        modelProductItem.quantity = item.Quantity.ToString();
                        modelProductItem.Spec = modelProductItem.barCode;
                        modelProductItem.preferPrice = Convert.ToDouble(item.SalePrice);
                        modelProductItem.costPrice = Convert.ToDouble(item.SalePrice);
                        modelProductItem.auditMemo = SellOrder.GetAuditMemo(item.UserRemark, item.YGD);
                        //如何判断barCode 条形码相同的情况下合并为一个商品quantity 订货数量相加

                        listProductItem.Add(modelProductItem);
}




------解决方案--------------------
代码仅供参考:

C# code

            Dictionary<string, Model.ProductItem> productDic = new Dictionary<string, Model.ProductItem>();
            List<Model.SellOrderCart> listOrderCart = SellOrderCart.GetModelList(OrderID);
            foreach (Model.SellOrderCart item in listOrderCart)
            {
                string barCode = SellOrder.GetSpec(item.Title, item.ItemCode, item.GoodsTypeID, item.GoodsID, item.Specs, "", item.YGD);
                if (productDic.ContainsKey(barCode))
                {
                    productDic[barCode].quantity += item.Quantity.ToString();
                }
                else
                {
                    Model.ProductItem modelProductItem = new Model.ProductItem();
                    modelProductItem.orderId = item.OrderID.ToString();
                    modelProductItem.barCode = barCode;
                    modelProductItem.Title = SellOrder.GetTitle(item.Title, item.SetTableID);
                    modelProductItem.Price = item.SalePrice.ToString();
                    modelProductItem.quantity = item.Quan