linq分组拼接字符串
Q:
id name
1 A
2 A
3 b
期望结果
{"1,2","A"}
{"3","b"}
A:
var tbl2 = from item in db.tbl select new {id=item.id.ToString(),item.name };
var result = from item in tbl2
group item by item.name into g
select g.Aggregate(
(workingSentence, next) =>
new { ids =workingSentence.id + "," +next.id , name = g.Key });
var result = from item in tbl2
group item by item.name into g
select g.Aggregate(
(workingSentence, next) =>
new { ids =workingSentence.id + "," +next.id , name = g.Key });
dushouke提供的答案
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 };
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 };