Results 1 to 6 of 6

Thread: Compare time with CurrentTime in Entity Framwork

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    7

    Compare time with CurrentTime in Entity Framwork

    Hi.

    I have a table in my database named "appointments" with {id_app, date, time,id_person}

    what i want to do is to select appointments where time is greater than currenttime but is not working

    This is my code:
    Code:
     Dim context As New cabinetEntities
            Dim query = From app In context.appointments 
                             Where
                               app.time >= DateTime.Now.TimeOfDay
                                Order By app.time Descending
                                Select app
    
    
            ProfMed.dgvAppointments .DataSource = query
    Last edited by virusel2013; May 7th, 2012 at 05:11 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Compare time with CurrentTime

    What is the data type of app.time? Is it TimeSpan or is it DateTime? Are you only interested in comparing the times and not the dates?

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    7

    Re: Compare time with CurrentTime

    In my SQL Server, the data type for app.time is "time(7)". Yes i want to compare only times.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Compare time with CurrentTime in Entity Framwork

    What exactly does "not working" mean? What actually happens? It looks like you're assigning an IEnumerable to the DataSource of a DataGridView, which wouldn't work regardless. WinForms data-binding requires an IList, so you would need to call ToList or, preferably, ToArray on your query.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    7

    Re: Compare time with CurrentTime in Entity Framwork

    The specified type member 'TimeOfDay' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
    I want to compare the time stored in database with currenttime to select only future appointments and not passed appoiments.

    i think that syntax
    vb Code:
    1. app.time >= DateTime.Now.TimeOfDay
    is not corect

    i tried to declare a variable
    Code:
     Dim current_time>= DateTime.Now.ToString("hh:mm:ss")
    and in query i modify
    Code:
    app.time=current_time
    and i get this error
    Code:
    Error	1	Overload resolution failed because no accessible '>=' can be called with these arguments:
        'Public Shared Operator =(a As String, b As String) As Boolean': Value of type 'System.TimeSpan?' cannot be converted to 'String'.
        'Public Shared Operator =(t1 As System.TimeSpan, t2 As System.TimeSpan) As Boolean': Value of type 'String' cannot be converted to 'System.TimeSpan'
    Last edited by virusel2013; May 7th, 2012 at 05:30 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Compare time with CurrentTime in Entity Framwork

    What you have there is valid LINQ syntax but, as the error message says, some things that are valid for VB and LINQ in general are not supported by LINQ to Entities specifically, i.e. the LINQ provider for the Entity Framework. That's because LINQ to Entities must translate your LINQ query into SQL code and it doesn't know how to do that in some cases, including DateTime.TimeOfDay.

    Try assigning the current time to a local TimeSpan variable and then using that variable in your LINQ query instead. I haven't tested it specifically but I think that that should work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width