Skip to content

DateTimeOffset to DateTime Null Conversion #20959

Closed

Description

@KSib

When doing a select query into a class with a Nullable from a database model class property that's a Nullable it seems the conversion wants to return a DateTime.MinValue. I'd expect all of the below tests to return null.

Steps to reproduce

void Main()
{
	var test1 = Coupons.Select(r => new
		{ r.EndDateTime }
	);

	foreach (var t1 in test1)
	{
		Console.WriteLine("Test1");
		Console.WriteLine(t1.EndDateTime);
		Console.WriteLine();
	}

	var test2 = Coupons.Select(c => new DTO
	{
		DT = c.EndDateTime == null ? (DateTime?)null : c.EndDateTime.Value.DateTime.Date
	});

	foreach (var t2 in test2)
	{
		Console.WriteLine("Test2");
		Console.WriteLine(t2.DT);
		Console.WriteLine();
	}

	var test3 = Coupons.ToList().Select(c => new DTO
	{
		DT = c.EndDateTime == null ? (DateTime?)null : c.EndDateTime.Value.DateTime.Date
	});

	foreach (var t3 in test3)
	{
		Console.WriteLine("Test3");
		Console.WriteLine(t3.DT);
		Console.WriteLine();
	}

}
public class DTO
{
	public DateTime? DT { get; set; }
}

// This is a database model class. There's also nothing in the constructor that modifies this property.
public class Coupons {
	public DateTimeOffset? EndDateTime { get; set; }
}

Console

Test1
null

Test2
1/1/0001 12:00:00 AM

Test3
null

Further technical details

EF Core version: 3.1.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Standard 2.1
Operating system: Windows 10 64bit
IDE: Visual Studio 2019 16.5.5

Activity

  1. added this to the 5.0.0 milestone on May 15, 2020
  2. smitpatel commented on May 16, 2020

    @smitpatel
    Contributor

    Same root cause as #20633

  3. added 3 commits that reference this issue on Jul 17, 2020
    078f68e
    4602ba3
    3ebfde7
  4. modified the milestones: 5.0.0, on Jul 18, 2020
  5. modified the milestones: , 5.0.0 on Nov 7, 2020
  6. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions