locked
Please I need help with this EF CORE Query RRS feed

  • Question

  • User-1204637165 posted

    Dear All,

    I am trying to read customers who have their birthdays today.

    var customers = await this.context.Customers.Where(item=>item.DOB != null && ((DateTime)(item.DOB.Value)).Day == currentdate.Day && ((DateTime)(item.DOB.Value)).Month == currentdate.Month).ToListAsync();

    Unfortunately it is not working.

     public class Customer
        {
            public int CustomerID { get; set; }
    
            [Column(TypeName = "Date")]
            public DateTime? DOB { get; set; } // Meaning date of Birth of the user
    
    }

    I dont know what the problem is.

    Monday, May 6, 2019 10:27 AM

Answers

  • User-1204637165 posted

    So I changed a simple clause in the Where clause and it works fine. This is the new modification I added

    item=>item.DOB.HasValue

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 10, 2019 12:53 AM

All replies

  • User753101303 posted

    Hi,

    What happens ? It fails with an error message or it's just doesn't return nothing?

    The next step would be to triple check your where criteria ie :
    - what is the value for currentdate.Day and currentdate.Month (maybe a UTC vs localtime issue ?)
    - triple check you do have values in your db that are supposed to match ?

    Monday, May 6, 2019 11:21 AM
  • User-1204637165 posted

    Hi Pat,

    It does not give any exception. It returns and empty result

    Monday, May 6, 2019 3:16 PM
  • User753101303 posted

    So more likely the Where clause is really wrong. Have you tried to triple check the values you are using for your query?

    Sometimes it is just that the query is mistakenly done against some other database than the one you think. I'll give this a try but it seems to me it should work.

    Monday, May 6, 2019 3:45 PM
  • User-1204637165 posted

    Thanks Pat,

    I would check and get back to you.

    Best Regards.

    JIde.

    Monday, May 6, 2019 3:52 PM
  • User-1204637165 posted

    So I changed a simple clause in the Where clause and it works fine. This is the new modification I added

    item=>item.DOB.HasValue

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 10, 2019 12:53 AM