Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to entities compare datetime with milliseconds precision

I have a specific linq to entity query that is written like the following:

var query = from orgs 
            in orgBaseQuery.Where(org => org.LastUpdated >= fromDate 
                                         && org.LastUpdated <= toDate)

The type of the toDate and fromDate are .net DateTime and the type of the LastUpdated is SQL DateTime. My problem is that the generated SQL query does not have the millisecond portion in it and that is causing some errors in our application. I was wondering if there is any solution to this problem without changing the data type of LastUpdated to datetime2?

like image 249
Mehdi Khaleghian Avatar asked Oct 15 '25 16:10

Mehdi Khaleghian


1 Answers

If your column in the database is datetime, then you're not going to have the precision that you're looking for. It is a legacy data type. Starting with SQL Server 2008 (possibly R2), the better data type to use is datetime2. It is a perfect match for the C# DateTime type, including precision.

like image 164
krillgar Avatar answered Oct 18 '25 05:10

krillgar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!