我有一组具有以下列的对象
Id
ShiftStart
ShiftEnd
一组示例数据
Id ShiftStart ShiftEnd
1 8.30 12.00
1 13.30 15.00
2 8.30 12.00
2 13.30 15.00
3 8.30 12.00
我想要实现的是,
选择具有匹配ID的所有项目,然后合并班次数据。用逗号分隔
因此,示例最终对象将包含以下数据
Id ShiftStart ShiftEnd
1 8.30, 13.30 12.00, 15.00
2 8.30, 13.30 12.00, 15.00
3 8.30 12.00
答案 0 :(得分:3)
按ID分组,然后在每个组内连接在一起:
var groupedData
= yourList.GroupBy(x => x.Id)
.Select(g => new { Id = g.Key,
ShiftStartTimes = string.Join(", ", g.Select(x => x.ShiftStart))
ShiftEndTimes = string.Join(", ", g.Select(x => x.ShiftEnd)) });
查询语法:
var groupedData =
from x in yourList
group x by x.Id into g
select new {
Id = g.Key,
ShiftStartTimes = String.Join(", ", g.Select(x => x.ShiftStart)),
ShiftEndTimes = String.Join(", ", g.Select(x => x.ShiftEnd))
};