我有一个可以为空的日期时间列表。我需要通过删除空值来将其过滤到不可为空的DateTime列表。
var d = NullableDateTimeList;
??//var nonNullableList = d.Where(p => p.DateX.HasValue).Select(p => p.Datex);
我该如何做到这一点。感谢。
答案 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();