none
Checking for Date Null with SqlFunctions RRS feed

  • Question

  • I want to check for null in my date function. If null show blank else show Date. I am doing like this but is not not working when null. When null it is showing //

    StartDate = SqlFunctions.DateName("day", x.StartDate).Trim() + "/" +
    SqlFunctions.StringConvert((double)x.StartDate.Value.Month).TrimStart() + "/" +
    SqlFunctions.DateName("year", x.StartDate) !=null ?
    SqlFunctions.DateName("day", x.StartDate).Trim() + "/" +
    SqlFunctions.StringConvert((double)x.StartDate.Value.Month).TrimStart() + "/" +					                                                                                             
    SqlFunctions.DateName("year", x.StartDate) : string.Empty,




    Ebenezer

    Tuesday, October 2, 2018 4:40 PM

Answers

  • Hi Zhanglong

    Thanks for your response. I have managed to solved the issue myself. Below is my solution. 

    StartDate = x.StartDate == null ? string.Empty : SqlFunctions.DateName("day", x.StartDate).Trim() + "/"
    + SqlFunctions.StringConvert((double)x.StartDate.Value.Month).TrimStart() + "/" +
    SqlFunctions.DateName("year", x.StartDate),


    Ebenezer

    • Marked as answer by denkyira Wednesday, October 3, 2018 12:02 PM
    Wednesday, October 3, 2018 12:02 PM

All replies

  • Hi denkyira,

    Could you please share the complete LINQ statement and related table structure, it will be beneficial to resolve the issue. 

    In addition, SqlFunctions will convert to database function in SQL statement, if the code used in C#, I would suggest that you could use C# function instead of SqlFunctions. 

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 3, 2018 2:18 AM
    Moderator
  • Hi Zhanglong

    Thanks for your response. I have managed to solved the issue myself. Below is my solution. 

    StartDate = x.StartDate == null ? string.Empty : SqlFunctions.DateName("day", x.StartDate).Trim() + "/"
    + SqlFunctions.StringConvert((double)x.StartDate.Value.Month).TrimStart() + "/" +
    SqlFunctions.DateName("year", x.StartDate),


    Ebenezer

    • Marked as answer by denkyira Wednesday, October 3, 2018 12:02 PM
    Wednesday, October 3, 2018 12:02 PM