日期:2014-05-18  浏览次数:20933 次

linq group 问题
ID Name GroupName
1 4x6 box 4x6
2 5x7 bag 5x7
3 4x6 container 4x6
4 5x7 box 5x7
5 4x6 bag 4x6
6 5x7 container 5x7
7 5x6 bottle 5x6
8 12x12 box 12x12
9 5x6 box 5x6
10 16x20 container 16x20

如何根据groupName把相关的ID放到一起,
结果得到一个2列的数据,

4x6 1,3,5
5x7 2,4,6
5x6 7,9
12x12 8
16x20 10





------解决方案--------------------
var query = from x in data
group x by x.groupName into g
select new { groupname = g.Key, items = string.Join(",", g.Select(x => x.ID).ToArray()) };
------解决方案--------------------
var query = from x in data
group x by x.groupName into g
select g.Key + string.Join("", g.Select(x => string.Format("<product productID='{0}' productName='{1}' />", x.ID, x.Name)).ToArray());