Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable DateTime List to Non Nullable DateTime List

Tags:

c#

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.

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

How do I go about doing this. Thanks.

like image 708
Bookamp Avatar asked Jun 11 '26 22:06

Bookamp


2 Answers

very close, just add the .Value property when selecting the nullable DateTime:

var nonNullableList = d.Where( p => p.DateX.HasValue )
    .Select( p => p.DateX.Value )
    .ToList();
like image 190
Moho Avatar answered Jun 14 '26 12:06

Moho


You have to select the Vaue from the Nullable<DateTime> before you call ToList():

List<DateTime> nonNulls = NullableDateTimeList
    .Where(d => d.DateX.HasValue)
    .Select(d => d.DateX.Value)
    .ToList();
like image 22
Tim Schmelter Avatar answered Jun 14 '26 11:06

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!