简体   繁体   中英

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

Assuming your DateTime s 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));

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM