Hi All
I have a C# console application written using Visual Studio 2008.
My system culture is en-GB. I have a Linq query that looks like this:
var myDate = "19-May-2010";
var cus = from x in _dataContext.testTable
where x.CreateDate == Convert.ToDateTime(myDate)
select x;
The resulting SQL query generates and error because it returns the dates as "19/05/2010" which it interprets as an incorrect date. For some reason even though my system culture is set to en-GB it looks like it's trying to intrepret it as a en-US date.
Any ideas how I get around this?
Edit: Thanks for the comments about magic strings and var abuse, but that's not my problem. My problem is that in the conversion from Linq to SQL the dates are being interpreted as US format dates (19/05/2010 is being interpreted as: month nineteen, day 5 and year 2010) resulting in the following error:
System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value
The where clause of the resulting SQL query looks like:
WHERE ([t0].[CreateDate] = '19/05/2010 00:00:00')
Please note that the exact same Linq query works perfectly in LinqPad.
I've tried the following where clause:
where x.CreateDate == DateTime.Today
and still get the error.
Additional Information:
SQL Server Query Visualizer:
SELECT [t0].[field1], [t0].[field2], [t0].[field3] AS [field4], [t0].[field5]
FROM [dbo].[table] AS [t0]
WHERE ([t0].[CreateDateTime] = '19/05/2010 00:00:00')
Original query:
SELECT [t0].[field1], [t0].[field2], [t0].[field3] AS [field4], [t0].[field5]
FROM [dbo].[table] AS [t0]
WHERE ([t0].[CreateDateTime] = @p0)
-------------------------------
@p0 [DateTime]: 19/05/2010 00:00:00
LINQPad:
-- Region Parameters
DECLARE @p0 DateTime SET @p0 = '2010-05-19 00:00:00.000'
-- EndRegion
SELECT [t0].[field1], [t0].[field2], [t0].[field3] AS [field4], [t0].[field5]
FROM [table] AS [t0]
WHERE ([t0].[CreateDateTime] = @p0)
In the above I notice that LinqPad presents the date in a different format to VS.
Thanks.
Alan T