实体类PubGoods:goodsId(商品ID), goodsQty(商品数量),prodate(生产日期),字段
现在有给集合List<PubGoods>中有无数字段
需求:将所有商品ID相同的,商品数量相加,生产日期以逗号隔开的方式存储,
例如:
goodsId:1 ;
goodsQty :500
prodate :2020-04-25,2022-04-25,2023-04-16
goodList
.GroupBy(p=>p.GoodsId)
.Select(g=>new PubGoods(){
goodsId=g.Key;
goodsQty=g.Sum(p=>p.goodsQty);
prodate=g.Select(p=>p.prodate).Aggregate((x,y)=>$"{x},{y}")
})
ConcurrentDictionary<string,Dto> data;
for(var good in goodList){
data.AddOrUpdate(
good.goodsId,
key=>new Dto(){goodsId=key, goodsQty=good.goodsQty, prodate=good.prodate},
(key,dto)=> {dto.goodsQty+=good.goodsQty; dto.prodate+=","+good.prodate}
)
}
//还可以并发跑.
goodList.AsParallel().ForAll(上面对字典的操作)
另外建个表插入,数据就用上面汇总。