加入对象列表中的元素

时间:2012-12-05 10:55:18

标签: c# list join linq-group

我有这堂课:

public class Note
{
    public DateTime Date { get; set; }
    public string Time { get; set; }
    public string Text { get; set; }
}

和一个清单

List<Note> ungroupedNotes;

我想要做的是将具有相同日期和时间的多个注释组合到一个注释中(它们的文本属性应该连接在一起,日期和时间相同)并输出新的

List<note> groupedNotes;

1 个答案:

答案 0 :(得分:1)

试试这个:

var groupedNotes = ungroupedNotes.GroupBy(x => new { x.Date, x.Time })
                                 .Select(x => new Note
                                              {
                                                  Date = x.Key.Date,
                                                  Time = x.Key.Time,
                                                  Text = string.Join(
                                                           ", ",
                                                           x.Select(y => y.Text))
                                              })
                                 .ToList();
相关问题