简体   繁体   中英

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.

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();
}
public List<DateTime> DistinctNoticeDates()
{
    return (from notices in this.GetTable<Notice>()
            orderby notices.Notice_DatePlanned descending
            select notices.Notice_DatePlanned.Date).Distinct().ToList();
}

You can use the Date property to strip of the time part of the DateTime :

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

Change you query to "cast" the dateTime to its Date part

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

Additionally, if you want to order them by their Date part only, i would order them after the distinct. This way you'll order a smaller list and thus increase performance

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

Try implement DateTime comparer which will compare dates inly by days (return true if days are equal) and use it as parameter for linq Distinct method. For example:

class DateTimeByDayComparer : IEqualityComparer<DateTime>
{
       public bool Equals(DateTime x, DateTime y)
       {
           return x.Day == y.Day;
       }
}

public List<DateTime> DistinctNoticeDates()
{
     var comparer = new DateTimeByDayComparer();
     return this.GetTable<Notice>().OrderByDescending(n => n.Notice_DatePlanned).Distinct(comparer).ToList();
}

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