gog
gog

Reputation: 12988

Compare two date properties of a list

I have two lists:

List<DateTime> monthWorkingDays

List<Note> monthUserNotes

In my Note class i have a DateTime NoteDate property (represents the date of note).

I need to check if the user didnt make any note in a working day to send a email to that user.

So i have to compare the NoteDate property with the monthWorkingDays data.

 public List<DateTime> GetIncompleteDays()
    {
        var days = GetWorkingDays();
        var incompleteDays = new List<DateTime>();
        var notes = GetLast30DaysNotesByUser(idUser).OrderBy(a => a.NoteDate);

            foreach (var note in notes)
            {
                //TODO
            }

        return incompleteDays;
    }

Any suggestions? Thanks

Upvotes: 1

Views: 983

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

Do not sort notes by date - sort selected incomplete days instead:

days.Select(d => d.Date)
    .Except(notes.Select(n => n.NoteDate))
    .OrderBy(d => d)

As Tim stated, you may not need to select d => d.Date if days do not have Time part. Consider to introduce some explanatory variables, to make solution more clear:

var workingDays = GetWorkingDays();
var notes = GetLast30DaysNotesByUser(idUser);
var daysWithNotes = notes.Select(n => n.NoteDate).Distinct();
var incompleteDays = workingDays.Except(daysWithNotes);

Upvotes: 1

Tim S.
Tim S.

Reputation: 56536

Assuming your DateTimes are comparable (if there is a time portion, you can remove that with the DateTime.Date property), this should work, using LINQ's Except:

var incompleteDays = days.Except(
         GetLast30DaysNotesByUser(idUser).Select(a => a.NoteDate));

Upvotes: 3

Related Questions