C# List对象数组合并

C# List对象数组合并

实例1:合并订单里的商品,注意这里是两次model,第一层是订单实体,第二层是商品实体

这里是测试用的model。

 public class TestModel: TestModel2
    {
        public string UserName { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string Address3 { get; set; }
        public int num { get; set; }
        public List<Goods> Goods { get; set; }
    }
    public class Goods
    {
        public string goodsName { get; set; }
        public decimal goodsPrice { get; set; }
        public int goodscount { get; set; }
    }
    public class TestModel2
    {
    //合并的订单数据(多个合并在一起的订单的订单号)
        public string orderids { get; set; }
    }

合并方法

Merge_Order : 合并订单方法
Merge_Goods:合并订单model中的商品


        public static List<TestModel2> Merge_Order(List<TestModel> tests)
        {

            List<TestModel2> Result = new List<TestModel2>();

			//这里是按照字段分组 可以用多字段进行分组
			//多字段分组时将p.UserName改为new { p.username,p.address1,p.address2 }
            var group = tests.GroupBy(p => p.UserName);
            
			//这里时循环分组进行数据重组
			//如果上面用的是多条件分组,这里的IGrouping就需要改为IGrouping
            foreach (IGrouping<string, TestModel> groupSequence in group)
            {
                TestModel mode = groupSequence.First();
                mode.Goods = Merge_Goods(groupSequence.Select(t =>
                     t.Goods).ToArray());
                TestModel2 m = mode;
                m.orderids = string.Join(",", groupSequence.Select(t =>
                     t.num).ToArray());
                Result.Add(m);
            }
            return Result;

            List<Goods> Merge_Goods(IEnumerable<List<Goods>> goods)
            {
                List<Goods> Result_goods = new List<Goods>();
                List<Goods> goods1 = new List<Goods>();
                foreach (List<Goods> items in goods)
                {
                    foreach (Goods item in items)
                    {	//取数所有商品model,方便后面进行合并
                        goods1.Add(item);
                    }

                }
                //这里测试的时候为了方便直接用商品名分组真正使用的时候肯定是不能这么用的
                //可以用商品的SKUID来进行分组,用来分组的数据必须是商品唯一的标识
                var group_goods = goods1.GroupBy(p => p.goodsName);
			
                foreach (IGrouping<string, Goods> groupSequence in group_goods)
                {
                    Goods mode = groupSequence.First();
                    mode.goodscount = groupSequence.Select(t =>
                          t.goodscount).Sum();
                   
                    Result_goods.Add(mode);
                }
                return Result_goods;
               
            }

        }

实例2:合并销售数据,这里就只有一个model

    //合并
    //返回数据存储实体
    List<M_Inca_sales_sum> _Sales_Sums = new List<M_Inca_sales_sum>();
    //查询出来的实体集合
     List<M_Inca_sales_sum> m_Inca_Sales_Sums=查询出来的数据实体集合
            var group = m_Inca_Sales_Sums.GroupBy(p => p.comefrom);

            //这里时循环分组进行数据重组
            foreach (IGrouping<string, M_Inca_sales_sum> groupSequence in group)
            {
                M_Inca_sales_sum mode = groupSequence.First();
                mode.orderNum = groupSequence.Select(t => t.orderNum).Sum();
                mode.arpa = groupSequence.Select(t => Convert.ToDecimal(t.arpa)).Sum().ToString();
                mode.sales = groupSequence.Select(t => Convert.ToDecimal(t.sales)).Sum().ToString();
                mode.maori = groupSequence.Select(t => Convert.ToDecimal(t.maori)).Sum().ToString();
                mode.grossmargin = groupSequence.Select(t => Convert.ToDecimal(t.grossmargin)).Sum().ToString();
                mode.nosalestax = groupSequence.Select(t => Convert.ToDecimal(string.IsNullOrWhiteSpace(t.nosalestax) ? "0" : t.nosalestax)).Sum().ToString();
                mode.notaxcost = groupSequence.Select(t => Convert.ToDecimal(string.IsNullOrWhiteSpace(t.notaxcost) ? "0" : t.notaxcost)).Sum().ToString();
                mode.notaxgrossmargin = groupSequence.Select(t => Convert.ToDecimal(string.IsNullOrWhiteSpace(t.notaxgrossmargin) ? "0" : t.notaxgrossmargin)).Sum().ToString();
                _Sales_Sums.Add(mode);
            }

你可能感兴趣的:(C#,c#)