C# linq分组拼接字符串

时间:2020-12-03 12:18:12   收藏:0   阅读:16

Q:

id  name
1   t
2   t
3   c

 

期望结果

{"1,2","t"}

{"3","c"}

 

方法如下:

var result = from item in tbl
    group item by item.name into g
    let ids = g.Select(b => b.id.ToString()).ToArray()
    select new { id = String.Join(",", ids), name = g.Key };

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!