C#之linq和lamda表达式GroupBy分组拼接字符串
业务需求:
点击提示信息,如:“售后单【SH001】序列号【001,002,006】;售后单【SH002】序列号【003,007,009】。已经过了质保期,确认要继续关闭吗”
核心代码:
//lamda表达式
var errorMsgObj = getNoPay.GroupBy(s => s["FBILLNO"] + "")
.Select(d=>new {
BillNo=d.Key,
NumberList=string.Join(",",d.Select(f=>f["FNUMBER"]+""))
}).ToList();
//linq
var resultMsg = (from item in getNoPay
group item by item["FBILLNO"] into g
let numbers = g.Select(b => b["FNUMBER"]).ToArray()
select new
{
BillNo = g.Key,
NumberList = string.Join(",", numbers)
}).ToList();
调试
集合数据:
展示数据: