query a Nullable Date RRS feed

  • Question

  • How do I query a nullable date in Linq?
    Here is my t-sql:
    select max(to_dt) from p_invc where year(to_dt)<2020

    Certified Geek

    Tuesday, October 27, 2015 1:35 PM


  • >>How do I query a nullable date in Linq?

    In your case, you could make a linq query as below:

    var res = (from t in db.Table
                               where t.TestDate.Value.Year < 2015
                               select t).ToList();

    Since the datetime column is nullable in database, it would generate a nullable property in the Entity class as well, as Nullable<DateTime> TestDate, we can't directly access the properties of DateTime. To get a concrete instance of DateTime we use the Value property on Nullable<DateTime> (all Nullable<T>s have this property) and this measure would help make sure it does not throw an null object reference exception.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Fred Bao Wednesday, November 4, 2015 3:09 AM
    • Marked as answer by Fred Bao Thursday, November 5, 2015 2:39 AM
    Wednesday, October 28, 2015 1:58 AM