Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Linq select distinct date time days

I have the following method which I planned to return a bunch of distinct date time objects. By distinct I means unique days (not including times).

The issue is, the the DateTime object have different times, and are therefore evaluating as unique even though they're the same day.

How can I have the query ignore the time part of the date and just evaulate the date for uniqueness?

    public List<DateTime> DistinctNoticeDates()
    {
        return (from notices in this.GetTable<Notice>()
                orderby notices.Notice_DatePlanned descending
                select notices.Notice_DatePlanned).Distinct().ToList();
    }

Thanks.

like image 683
Nick Avatar asked Mar 22 '23 09:03

Nick


1 Answers

Try using the Date property to get just the date of DateTime structure:

public List<DateTime> DistinctNoticeDates()
{
    return (from notices in this.GetTable<Notice>()
            orderby notices.Notice_DatePlanned descending
            select notices.Notice_DatePlanned.Date)
            .Distinct()
            .ToList();
}
like image 126
Felipe Oriani Avatar answered Mar 31 '23 17:03

Felipe Oriani