none
LINQ to entities - format date field RRS feed

  • Question

  • Hi.

    I have a linq to entities query that selects all fields in a database table for the current customer.

    I want to display the date as "dd/mm/yyyy" rather than the SQL server default - "yyyy-mm-dd hh-mm-ss"

    This is the query:

            Dim myQuery As IQueryable(Of tblCustomer) = From p In publishContext.tblCustomer
            Where p.CustomerID = selectedCustomerID
            Select p
    I need to select all fields in the table but format the date of birth field as "dd/mm/yyyy"

    Wednesday, August 6, 2014 3:27 PM

Answers

  • I think the best way to ask users to input dates is to use some form of DatePicker control

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Marked as answer by typicalalex1 Wednesday, August 6, 2014 3:54 PM
    Wednesday, August 6, 2014 3:53 PM

All replies

  • You should leave formatting of dates to your UI layer, and just get datetime data from the database.


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Wednesday, August 6, 2014 3:41 PM
  • I did originally but i couldn't get it to work in the way i wanted:

    <TextBox Text="{Binding DateLeft, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, TargetNullValue='', StringFormat=\{0:d\}}" Margin="585,293,0,0" Width="93"/>

    This does show the date in the way i want, but its really annoying when you want to insert or change the date.

    If you type a new date and type 12/12, it automatically puts in 2014 at the end, so you have to go back and delete it in order to put another year in, or if you delete the last number, it changes to 12/12/0201.

    How do i display the date in the correct format without it checking what i type as im typing it?

    Hope that makes sense

    Wednesday, August 6, 2014 3:47 PM
  • I think the best way to ask users to input dates is to use some form of DatePicker control

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Marked as answer by typicalalex1 Wednesday, August 6, 2014 3:54 PM
    Wednesday, August 6, 2014 3:53 PM
  • I've come to the same conclusion.

    Thanks

    Wednesday, August 6, 2014 3:54 PM