This is a migrated thread and some comments may be shown as answers.

Converting to DateTime in where clause

6 Answers 2271 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Libertad
Top achievements
Rank 1
Libertad asked on 30 Jan 2013, 05:43 PM
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.

6 Answers, 1 is accepted

Sort by
0
Viktor Zhivkov
Telerik team
answered on 01 Feb 2013, 01:55 PM
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.
0
Libertad
Top achievements
Rank 1
answered on 01 Feb 2013, 03:43 PM
Thanks Viktor,
but on backend I'm using Oracle. Is that work in this situation too?
0
Ralph Waldenmaier
Telerik team
answered on 04 Feb 2013, 12:16 PM
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.
0
Stephen
Top achievements
Rank 1
answered on 13 Nov 2014, 11:04 PM
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
0
Ralph Waldenmaier
Telerik team
answered on 14 Nov 2014, 08:26 AM
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.
 
0
Bruno Carlos
Top achievements
Rank 1
answered on 08 May 2016, 04:32 PM
Please tell me where i can find this extension methods, String.SQL<T>, I need this ASAP.
Tags
LINQ (LINQ specific questions)
Asked by
Libertad
Top achievements
Rank 1
Answers by
Viktor Zhivkov
Telerik team
Libertad
Top achievements
Rank 1
Ralph Waldenmaier
Telerik team
Stephen
Top achievements
Rank 1
Bruno Carlos
Top achievements
Rank 1
Share this question
or