Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从datetime集合获取不同的非空日期_C#_.net_Linq_Collections_Lambda - Fatal编程技术网

C# 从datetime集合获取不同的非空日期

C# 从datetime集合获取不同的非空日期,c#,.net,linq,collections,lambda,C#,.net,Linq,Collections,Lambda,我收集了一组日期时间,如下所示: IEnumerable<DateTime?> dates IEnumerable日期 我想通过降序得到不同日期的集合 IEnumerable<DateTime> dtCollection = dates.Where(x => x.HasValue).Distinct().OrderByDescending(x => x).AsEnumerable(); IEnumerable dtCollection=dates.

我收集了一组日期时间,如下所示:

IEnumerable<DateTime?> dates
IEnumerable日期
我想通过降序得到不同日期的集合

   IEnumerable<DateTime> dtCollection = dates.Where(x => x.HasValue).Distinct().OrderByDescending(x => x).AsEnumerable();
IEnumerable dtCollection=dates.Where(x=>x.HasValue).Distinct().OrderByDescending(x=>x).AsEnumerable();
在上面的代码中,我得到一个invalid cast异常,distinct返回distinct(日期+时间),而不是distinct日期

因此:

  • 为什么
    Where(x=>x.HasValue)
    没有放弃所有空值
  • 如何修复代码以完成任务

  • 谢谢,

    在查询中,通过选择
    DateTime?
    对象的值,将其转换为
    DateTime

    IEnumerable<DateTime> dtCollection = dates
        .Where(x => x.HasValue)
        .Select(x => x.Value)
        .Distinct()
        .OrderByDescending(x => x)
        .AsEnumerable();
    

    在查询中,通过选择
    DateTime?
    对象的值,将其转换为
    DateTime

    IEnumerable<DateTime> dtCollection = dates
        .Where(x => x.HasValue)
        .Select(x => x.Value)
        .Distinct()
        .OrderByDescending(x => x)
        .AsEnumerable();
    

    在查询中,通过选择
    DateTime?
    对象的值,将其转换为
    DateTime

    IEnumerable<DateTime> dtCollection = dates
        .Where(x => x.HasValue)
        .Select(x => x.Value)
        .Distinct()
        .OrderByDescending(x => x)
        .AsEnumerable();
    

    在查询中,通过选择
    DateTime?
    对象的值,将其转换为
    DateTime

    IEnumerable<DateTime> dtCollection = dates
        .Where(x => x.HasValue)
        .Select(x => x.Value)
        .Distinct()
        .OrderByDescending(x => x)
        .AsEnumerable();
    

    您可以使用.Date获取DateTime的日期组件,以便:

    dates
        .Where(x => x.HasValue)
        .Select(x => x.Value.Date)
        .Distinct()
        .OrderByDescending(x => x)
    

    作为对第一点的回答,
    Where(x=>x.HasValue)
    按照您的预期丢弃所有空值,但您仍然保留了
    DateTime?
    的集合,而不是
    DateTime
    ,当您尝试将其分配给
    IEnumerable dtCollection
    时,会导致类型转换错误,因此,您需要使用
    x.Value
    将每个
    DateTime?
    转换为
    DateTime
    ,您可以使用.Date来获取DateTime的日期组件,因此:

    dates
        .Where(x => x.HasValue)
        .Select(x => x.Value.Date)
        .Distinct()
        .OrderByDescending(x => x)
    

    作为对第一点的回答,
    Where(x=>x.HasValue)
    按照您的预期丢弃所有空值,但您仍然保留了
    DateTime?
    的集合,而不是
    DateTime
    ,当您尝试将其分配给
    IEnumerable dtCollection
    时,会导致类型转换错误,因此,您需要使用
    x.Value
    将每个
    DateTime?
    转换为
    DateTime
    ,您可以使用.Date来获取DateTime的日期组件,因此:

    dates
        .Where(x => x.HasValue)
        .Select(x => x.Value.Date)
        .Distinct()
        .OrderByDescending(x => x)
    

    作为对第一点的回答,
    Where(x=>x.HasValue)
    按照您的预期丢弃所有空值,但您仍然保留了
    DateTime?
    的集合,而不是
    DateTime
    ,当您尝试将其分配给
    IEnumerable dtCollection
    时,会导致类型转换错误,因此,您需要使用
    x.Value
    将每个
    DateTime?
    转换为
    DateTime
    ,您可以使用.Date来获取DateTime的日期组件,因此:

    dates
        .Where(x => x.HasValue)
        .Select(x => x.Value.Date)
        .Distinct()
        .OrderByDescending(x => x)
    

    作为对第一点的回答,
    Where(x=>x.HasValue)
    按照您的预期丢弃所有空值,但您仍然保留了
    DateTime?
    的集合,而不是
    DateTime
    ,当您尝试将其分配给
    IEnumerable dtCollection
    时,会导致类型转换错误,因此,您需要使用
    x.Value
    将每个
    DateTime?
    转换为
    DateTime

    @grantwiney我想忽略时间,我只需要为您的
    1
    设置一个不同的日期,因为没有人回答它:因为您这样做了
    HasValue
    ,实际上,您并没有将
    DateTime?
    转换为
    DateTime
    。它只是获取所有非空的项。因此,您需要从这些项中获取
    值,如这些答案所示。@Grantwney我想忽略时间,我只需要为您的
    1
    指定一个不同的日期,因为没有人回答它:仅仅因为您有
    HasValue
    ,您实际上并没有将
    DateTime?
    转换为
    DateTime
    。它只是获取所有非空的项。因此,您需要从这些项中获取
    值,如这些答案所示。@Grantwney我想忽略时间,我只需要为您的
    1
    指定一个不同的日期,因为没有人回答它:仅仅因为您有
    HasValue
    ,您实际上并没有将
    DateTime?
    转换为
    DateTime
    。它只是获取所有非空的项。因此,您需要从这些项中获取
    值,如这些答案所示。@Grantwney我想忽略时间,我只需要为您的
    1
    指定一个不同的日期,因为没有人回答它:仅仅因为您有
    HasValue
    ,您实际上并没有将
    DateTime?
    转换为
    DateTime
    。它只是获取所有非空的项。因此,您需要从这些项目中获取
    ,如这些答案所示。