可空日期时间列表到不可为空的日期时间列表

时间:2014-10-20 20:16:46

标签: c#

我有一个可以为空的日期时间列表。我需要通过删除空值来将其过滤到不可为空的DateTime列表。

var d = NullableDateTimeList;
??//var nonNullableList = d.Where(p => p.DateX.HasValue).Select(p => p.Datex);

我该如何做到这一点。感谢。

3 个答案:

答案 0 :(得分:2)

非常接近,只需在选择可空.Value时添加DateTime属性:

var nonNullableList = d.Where( p => p.DateX.HasValue )
    .Select( p => p.DateX.Value )
    .ToList();

答案 1 :(得分:1)

只需选择.Value

即可
var nonNullableList = d.Where(p => p.DateX.HasValue).Select(p => p.DateX.Value);

答案 2 :(得分:1)

在致电Vaue之前,您必须从Nullable<DateTime>中选择ToList()

List<DateTime> nonNulls = NullableDateTimeList
    .Where(d => d.DateX.HasValue)
    .Select(d => d.DateX.Value)
    .ToList();