1

I have a LINQ query which checks to see if there are any tests done since the beginning of the year.

var MetersTestedCount = (from n in _mttDomainContext.MTTMeterTests
                              where n.TestDate > DateTime.Parse("1/1/2010")  
                              select n.TestDate).Count();

This query however returns an empty set. I have a similar SQL query which pulls some records,

USE MeterTestTracking
Select * From MTTMeterTest
WHERE TestDate > '1/1/2010'

I have been to the previous posts. Even though similar, still no help:

How to compare just the date, not the timestamp using LINQ

and

How to compare dates in LINQ?

What's the correct way to check dates in LINQ to return a dataset?

Community
  • 1
  • 1
Jacob Saylor
  • 2,371
  • 1
  • 31
  • 46

4 Answers4

5

Have you tried creating a DateTime instance?:

var MetersTestedCount = (from n in _mttDomainContext.MTTMeterTests
                              where n.TestDate > new DateTime(2010,1,1).Date  
                              select n.TestDate).Count();
Thomas
  • 63,911
  • 12
  • 95
  • 141
  • @Redburn - Using SQL Profiler, what is the query from the above code that was sent to the server? – Thomas May 03 '10 at 05:48
  • Thanks @Thomas, I was looking for a way to remove functions from the where clause in the generated SQL when comparing dates. Comparing the Entity.DateTime property to the 'Date' property of the newly created DateTime object did it for me. – VFein Aug 01 '12 at 15:50
0

How about explicitly stating the format of the date string you're converting i.e. use

DateTime.Parse("1/1/2010", "dd/MM/YYYY")
Raj
  • 1,742
  • 1
  • 12
  • 17
0

Run SQL profiler (if you have MS SQL 2005 or higher) and check what queries are executed. Or try Linq2Sql visualizer which shows you generated SQL and/or results.

Karel Frajták
  • 4,389
  • 1
  • 23
  • 34
0

var query = Set().AsQueryable();
query = query.Where(r => (DateTime)(r.StartDate) > (DateTime)(obj.StartDate));