通过linq查询将字符串添加到一起

时间:2014-04-08 16:33:27

标签: linq

我正在尝试组合这些值,如果有多个所有者ID,它会将行值添加到一起。

http://i.snag.gy/fOWWQ.jpg

使用图像Given,如果我的查询已经运行,我最终会以

结束

http://i.snag.gy/qO1DO.jpg

到目前为止我尝试了什么

from Person in TargetGroups
group Person by Person.PersonId into g
select new {g.PersonId, g.Value}

1 个答案:

答案 0 :(得分:2)

您只需要连接Car列的值,如下所示:

TargetGroups
.GroupBy(x => x.PersonId)
.Select(x => new { PersonId = x.Key, Cars = string.Join(",", x.Select(a => a.Car)) });

查询语法:

from Person in TargetGroups
group Person by Person.PersonId into g
select new {  PersonId =  g.Key, Cars = string.Join(",", g.Select(x => x.Car)) }