简体   繁体   English

可空的DateTime列表到不可空的DateTime列表

[英]Nullable DateTime List to Non Nullable DateTime List

I have a list of nullable date times. 我有一个可为空的日期时间列表。 I need to filter it down to a non-nullable DateTime list by excuding the null values. 我需要通过排除空值将其过滤为不可空的DateTime列表。

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

How do I go about doing this. 我该怎么做。 Thanks. 谢谢。

very close, just add the .Value property when selecting the nullable DateTime : 非常接近,选择可空的DateTime时只需添加.Value属性即可:

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

只需选择.Value

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

You have to select the Vaue from the Nullable<DateTime> before you call ToList() : 在调用ToList()之前, VaueNullable<DateTime>选择Vaue

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM