0

I have this class:

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

and a list

List<Note> ungroupedNotes;

What I want to do is group multiple notes that have the same Date and Time into a single Note (their Text properties should be joined together, Date and Time are the same) and output a new

List<note> groupedNotes;
ekad
  • 14,436
  • 26
  • 44
  • 46
Cătălin Rădoi
  • 1,804
  • 23
  • 43

1 Answers1

1

Try this:

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();
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443