none
How to programatically Date Format a linq query?

    Question

  • Basically I have the following code and it retrieves data fine, however, the date field comes back in the long format (with time) when I really want the short format without the time string.  IS there a way to format the start end end date field on this query?.  I know how to do it when using a Linq data source, but I rather do it thru the query if possible.  Or perhaps I need to do it on a gridview event like after the date field is bound.  Anyone?

    if

     

    (prjID != 0)

    {

    gv2.DataSource = (

    from i in dc.Iterations

     

    where i.PID == prjID

     

    select new

    {

    i.IterationID,

    i.IterationName,

    i.ValidationManager,

    i.TestLocation,

    i.StartDate,

    i.EndDate,

    i.NumbTechs });

    gv2.DataBind();


    NR
    Monday, September 07, 2009 8:06 PM

Answers

  • Hi NR,

     

    If you want a short time format string values in the anonymous type, you can consider Veera’s suggestion to call DateTime.ToString(dd/mm/yyyy).  Also, we can use DateTime.ToShortDateString method.   The reason why we cannot call the Date property more than once in anonymous type is because we cannot have two properties with the same name.  In anonymous definition, if we don’t explicitly specify the property name, the compiler gives the anonymous type members the same name as the property being used to initialize them.  (http://msdn.microsoft.com/en-us/library/bb397696.aspx)  So we can explicitly specify the properties names avoid the duplicate. 

    ==================================================================
    from i in dc.Iterations

    where i.PID == prjID

    select new

    {

        i.IterationID,

    i.IterationName,

    i.ValidationManager,

    i.TestLocation,

    StartDate = i.StartDate.ToShortDateString(),

    EndDate = i.EndDate.ToShortDateString(),

    i.NumTechs

    };
    ==================================================================

     

    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Highlander4 Wednesday, September 09, 2009 12:12 AM
    Tuesday, September 08, 2009 1:23 AM
    Moderator

All replies

  • Just change your query to return the DateTime.Date property of the DateTime, instead of the entire DateTime:

    from i in dc.Iterations
    where i.PID == prjID
    select new
    {
        i.IterationID,
        i.IterationName,
        i.ValidationManager,
        i.TestLocation,
        i.StartDate.Date,
        i.EndDate.Date,
        i.NumbTechs 
    }

    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, September 07, 2009 8:37 PM
    Moderator
  • This didn't work.  It returns the same identical DateTime values and since I'm using anonimous types, I can't use the .date more than once in the same query.
    NR
    Monday, September 07, 2009 10:16 PM
  • Hi NR,

     

    If you want a short time format string values in the anonymous type, you can consider Veera’s suggestion to call DateTime.ToString(dd/mm/yyyy).  Also, we can use DateTime.ToShortDateString method.   The reason why we cannot call the Date property more than once in anonymous type is because we cannot have two properties with the same name.  In anonymous definition, if we don’t explicitly specify the property name, the compiler gives the anonymous type members the same name as the property being used to initialize them.  (http://msdn.microsoft.com/en-us/library/bb397696.aspx)  So we can explicitly specify the properties names avoid the duplicate. 

    ==================================================================
    from i in dc.Iterations

    where i.PID == prjID

    select new

    {

        i.IterationID,

    i.IterationName,

    i.ValidationManager,

    i.TestLocation,

    StartDate = i.StartDate.ToShortDateString(),

    EndDate = i.EndDate.ToShortDateString(),

    i.NumTechs

    };
    ==================================================================

     

    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Highlander4 Wednesday, September 09, 2009 12:12 AM
    Tuesday, September 08, 2009 1:23 AM
    Moderator