An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Hello @Corey Fleig ,
I suspect your entity class (the one mapped to your database table) likely has a non-nullable DateTime property. You should look for the entity class that your myTable DbSet uses:
public class YourTableEntity
{
public int Id { get; set; }
public DateTime? somedate { get; set; } // ← Added nullable
}
And the result class should correctly define the DateTime property as nullable as well:
public class myClass
{
public DateTime? MyDate { get; set; }
}
Everything else is the same as before, you can even query for NULL values without any issues:
var query = from c in myTable
where Id == 1
select new myClass { myDate = c.somedate };
var result = query.FirstOrDefault();
if (result?.myDate.HasValue == true)
{
Console.WriteLine($"Date: {result.myDate.Value}");
}
else
{
Console.WriteLine("Date is NULL");
}
var nullRecords = from c in myTable
where c.somedate == null
select new myClass { myDate = c.somedate };
If you still face issues, please share the relevant parts of your entity class and the result class so I can help you further.