How to use DateTime in LINQ when it's NULL

Corey Fleig 615 Reputation points
2026-11-13T02:27:18.8333333+00:00

create table mytable (Birth datetime null)

public class myModel {

public Datetime Birth { get; set; }

}

var query = from aa in mydb.mytable

select new cmyModel

{

 Birth = aa.Birth

}

I get a message: Nullable object must have a value;

If I make the data type in the model nullable (DateTime?) that will work,

but in my view I have

 var dob = Birth.ToShortDateString();

and that will not work at all if my model is nullable. So what's the best way to correct this?

Developer technologies | .NET | Entity Framework Core
0 comments No comments

Answer accepted by question author
Nguyen Dam (WICLOUD CORPORATION) 1,420 Reputation points Microsoft External Staff Moderator
2026-11-13T09:18:11.0066667+00:00

Hello @Corey Fleig and thank you for your question.

When formatting the date, you can use the null-conditional operator (?.) to call ToShortDateString() only when Birth has a value:


var dob = Birth?.ToShortDateString();

Just make sure that Birth is declared as a nullable DateTime (DateTime?) in your model as well.

Also, depending on what you want to do with dob, you could consider using the null-coalescing operator (??) to provide a fallback value, or use an explicit HasValue check:


var dob = Birth?.ToShortDateString() ?? "";

var dob = Birth.HasValue
    ? Birth.Value.ToShortDateString()
    : "";

I hope this helps, and please let me know if you have other questions.

If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide for your confirmation. Thank you. 

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Oldest
  1. AI answer

    2026-11-13T02:27:54.9933333+00:00

    Make Birth nullable in both the EF Core entity and the view model, then use null-conditional formatting in the view so a missing birth date renders as an empty string.

    Entity and view model

    A nullable SQL datetime column should map to DateTime?; EF Core treats nullable value types as optional properties and non-nullable value types as required properties.^1^

    // EF Core entity mapped to mytable
    public class MyTable
    {
        public DateTime? Birth { get; set; }
    }
    
    // Model passed to the view
    public class MyModel
    {
        public DateTime? Birth { get; set; }
    }
    

    Ensure the mapped entity property is nullable—not only the projected view-model property. Otherwise, EF Core can fail while materializing the database row, before assigning the result to MyModel.^2^

    The projection then remains straightforward:

    var query =
        from aa in mydb.MyTable
        select new MyModel
        {
            Birth = aa.Birth
        };
    

    Format it safely in the view

    Use ?. to call ToShortDateString() only when Birth has a value, and ?? to specify what should be displayed when it is null:^3^

    var dob = Model.Birth?.ToShortDateString() ?? "";
    

    Or directly in a Razor view:

    <span>@(Model.Birth?.ToShortDateString() ?? "")</span>
    

    To display a label instead of an empty value:

    <span>@(Model.Birth?.ToShortDateString() ?? "Not provided")</span>
    

    An equivalent explicit check is:

    var dob = Model.Birth.HasValue
        ? Model.Birth.Value.ToShortDateString()
        : "";
    

    Do not use Birth.Value.ToShortDateString() without first checking HasValue; accessing Value when the nullable date is empty throws InvalidOperationException with “Nullable object must have a value.”^4^

    If a missing birth date is valid business data, preserving it as null is preferable to replacing it with an arbitrary date such as DateTime.MinValue.


    References

    1. Entity Properties
    2. LINQ returns System.Data.SqlTypes.SqlNullValueException. Why? - Microsoft Q&A
    3. C# null safety
    4. System.Nullable<T> structure
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.