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
。它只是获取所有非空的项。因此,您需要从这些项目中获取值
,如这些答案所示。