Take a look at below example, which should help in resolving an issue.
void Main()
{
List<TableDE> data = new List<TableDE>()
{
new TableDE(){IdFirst=1, IdSecond=1, Name="name11"},
new TableDE(){IdFirst=1, IdSecond=1, Name="name12"},
new TableDE(){IdFirst=2, IdSecond=2, Name="name21"},
new TableDE(){IdFirst=2, IdSecond=2, Name="name22"},
new TableDE(){IdFirst=3, IdSecond=3, Name="name31"},
new TableDE(){IdFirst=1, IdSecond=2, Name="name1221"}
};
var result = data
.GroupBy(x=> new{ Id1 = x.IdFirst, Id2 = x.IdSecond})
.Select(grp=> new
{
IdFirst = grp.Key.Id1,
IdSecond = grp.Key.Id2,
Names = string.Join(",", grp.OrderBy(y=>y.Name).Select(y=>y.Name)),
})
.OrderBy(x=>x.IdFirst)
.ThenBy(x=>x.IdSecond)
.ToList();
result.Dump();
}
public class TableDE
{
public int IdFirst { get; set; }
public int IdSecond { get; set; }
public string Name { get; set; }
}
Result:
IdFirst IdSecond Names
1 1 name11,name12
1 2 name1221
2 2 name21,name22
3 3 name31