C#中的LINQ选择不同的日期和时间天 [英] c# Linq select distinct date time days

查看:592
本文介绍了C#中的LINQ选择不同的日期和时间天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我计划返回一堆不同的日期和时间对象的下面的方法。通过不同的我是指独特的天数(不包括时间)。

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

问题是,在DateTime对象有不同的时间,并因此评估,即使他们是同一天,唯一的。

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

我怎样才能让查询忽略日期的时间部分,只是evaulate日期的唯一性?

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

感谢。

推荐答案

尝试使用的 日期 属性来获取的DateTime 结构只是日期:

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

这篇关于C#中的LINQ选择不同的日期和时间天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆