using System;
using System.Collections.Generic;
using System.Linq;
public class MyData
{
public int Id { get; set; }
public string Name { get; set; }
public int Floor { get; set; }
}
public class MyGroupedData
{
public string Name { get; set; }
public IEnumerable<int> Floors { get; set; }
}
public class Test
{
public static void Main()
{
MyData[] data = {
new MyData() { Id = 1, Name = "Store1", Floor = 1 },
new MyData() { Id = 2, Name = "Store2", Floor = 1 },
new MyData() { Id = 3, Name = "Store2", Floor = 2 },
new MyData() { Id = 4, Name = "Store2", Floor = 3 },
new MyData() { Id = 5, Name = "Store3", Floor = 2 },
};
Console.WriteLine("1");
var groupedData = from x in data group x by x.Name into grp
select new MyGroupedData() { Name = grp.Key, Floors = grp.Select(y => y.Floor) };
Console.WriteLine("2");
foreach(var g in groupedData)
Console.WriteLine("{0} -> {1}", g.Name, string.Join(", ", g.Floors.Select(x => x.ToString()).ToArray()));
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkxpbnE7CgpwdWJsaWMgY2xhc3MgTXlEYXRhCnsKCXB1YmxpYyBpbnQgSWQgeyBnZXQ7IHNldDsgfQoJcHVibGljIHN0cmluZyBOYW1lIHsgZ2V0OyBzZXQ7IH0KCXB1YmxpYyBpbnQgRmxvb3IgeyBnZXQ7IHNldDsgfQp9CgpwdWJsaWMgY2xhc3MgTXlHcm91cGVkRGF0YQp7CglwdWJsaWMgc3RyaW5nIE5hbWUgeyBnZXQ7IHNldDsgfQoJcHVibGljIElFbnVtZXJhYmxlPGludD4gRmxvb3JzIHsgZ2V0OyBzZXQ7IH0KfQoKcHVibGljIGNsYXNzIFRlc3QKewoJcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQoJewoJCU15RGF0YVtdIGRhdGEgPSB7IAoJCQluZXcgTXlEYXRhKCkgeyBJZCA9IDEsIE5hbWUgPSAiU3RvcmUxIiwgRmxvb3IgPSAxIH0sIAoJCQluZXcgTXlEYXRhKCkgeyBJZ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