Converting to DateTime in where clause

7 posts, 0 answers
  1. Libertad
    Libertad avatar
    53 posts
    Member since:
    Sep 2008

    Posted 30 Jan 2013 Link to this post

    Following converting to DateTime doesn't work. any body know the solution?

    var q = DataContext.LOCATIONs.Where(Convert.ToDateTime(m.CL8) >= startDateTime );
    CL8 is string.
  2. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 01 Feb 2013 Link to this post

    Hi Libertad,

    You can modify your query in the following way:
    1.var q = DataContext.LOCATIONs.Where(m=> "convert(datetime, {0}) >= {1}".SQL<bool>(m.CL8, startDateTime));

    For now the best way to handle this specific type conversion is to use the SQL<T>() extensions method that allows you to write backend-dependent SQL code snippets and insert them in your LINQ queries.
    Please note that the SQL statement in this case is specific to MS SQL Server and can fail if different backend is used.

    Kind regards,
    Viktor Zhivkov
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  3. DevCraft banner
  4. Libertad
    Libertad avatar
    53 posts
    Member since:
    Sep 2008

    Posted 01 Feb 2013 Link to this post

    Thanks Viktor,
    but on backend I'm using Oracle. Is that work in this situation too?
  5. Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 04 Feb 2013 Link to this post

    Hello Libertad,

    You can call the TO_DATE function in oracle to convert string values to datetime values. This would look like this:

    var q = DataContext.LOCATIONs.Where(m => "to_date({0}, 'yyyy/mm/dd') >= {1}".SQL<bool>(m.CL8, startDateTime));
    You can find more details about the TO_DATE function here.

    I hope this information is helpful for you.
    Do come back in case you need further assistance.
     Greetings,
    Ralph
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  6. Stephen
    Stephen avatar
    1 posts
    Member since:
    Nov 2014

    Posted 13 Nov 2014 in reply to Ralph Waldenmaier Link to this post

    Ralph,

    Could you please tell me the namespace in which to find the SQL extension method to which you refer?  I've ransacked the Internet and can find no mention of such.

    Regards,

    Stephen
  7. Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 14 Nov 2014 Link to this post

    Hello Stephen,
    The namespace is Telerik.OpenAccess.

    using Telerik.OpenAccess

    Hope this helps.
    Feel free to ask in case you have any other question.

    Regards,
    Ralph Waldenmaier
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  8. Bruno
    Bruno avatar
    1 posts
    Member since:
    May 2016

    Posted 08 May in reply to Viktor Zhivkov Link to this post

    Please tell me where i can find this extension methods, String.SQL<T>, I need this ASAP.
Back to Top
DevCraft banner