question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LinqToDb produces a query that wraps Int64 DateTime.Ticks value in SQLite DateTime() function?

See original GitHub issue

Hello, I’ve changed my code recently to store any DateTime values as Int64 values by setting up my DataConnection like this:

            var connection = new DataConnection(ProviderName.SQLiteMS, connectionString)
            {
                InlineParameters = true
            };

            connection.MappingSchema.SetDataType(typeof(DateTime), DataType.Int64);
            connection.MappingSchema.SetConvertExpression(
                (long ticks) => new DateTime(ticks, DateTimeKind.Unspecified)
            );
            connection.MappingSchema.SetConvertExpression(
                (DateTime dt) => dt.Ticks
            );
            connection.MappingSchema.SetValueToSqlConverter(
                typeof(DateTime),
                (sb, dType, v) => sb.Append(((DateTime) v).Ticks.ToString(CultureInfo.InvariantCulture))
            );

and that works fine for me so long as I don’t apply a WHERE to any queries I do. When I do apply a WHERE, this is an example of a query produced:

SELECT
        [x].[DateTime],
        [x].[Value]
FROM
        [fae5697f79034df99e0ad32292446f5a_testOut] [x]
WHERE
        DateTime([x].[DateTime]) > DateTime(637304378093620000) AND
        DateTime([x].[DateTime]) < DateTime(637304382196380000)

The WHERE condition is created using Linq.Expressions (small but hopefully sufficient cut of code):

var left = Expression.Property(x, p);
var right = Expression.Constant(dt, typeof(DateTime));
var e = Expression.GreaterThan(left, right);
var whereCondition = Expression.Lambda<Func<T, bool>>(e, x);

...
queryable = queryable.Where(whereCondition);
...

Sorry for not providing more code or any mapping classes. All the mapping classes I’m using are generated dynamically at run time with IL.Emit so I don’t have any more code to give that wouldn’t make things more confusing. If necessary I can try making a solution that reproduces the problem, but I’m hoping that I’m just missing something like an extra setting on the DataConnection that I wasn’t able to find and you can see the problem easily.

linq2db v2.9.8 linq2db.SQLite.MS Ubuntu 18.04 .NET Core 3.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Jul 15, 2020

Looking into DateTime conversion and inlining problem. Looks like there are legacy conversions.

0reactions
Edward-Alchikhacommented, Jul 15, 2020

@Edward-Alchikha, can you live for several days with parameters? I’ll fix inlining.

Yea, the inlining problem doesn’t bother me since I can just use my original code that properly inlines since neither set of code works for not calling the SQLite DateTime() function on my Int64 DateTime.Ticks values anyways.

I can manage without the DateTime.Ticks fix for now as well, since that’s on an experimental branch, but it would be nice to have that working since it improves the size of the database files and the performance (in the case of a simple insert or read with no WHERE)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use a DateTime in a where clause in Sqlite.Net
Try this: string dateToday = DateTime.Today; var validDates = Database.Connection.Table<Dates>().Where(x => x.StartDate <= dateToday && x.
Read more >
Date And Time Functions
All six date and time functions take an optional time value as an argument, ... The datetime() function returns the date and time...
Read more >
Function Mappings - SQLite Database Provider - EF Core
Function Mappings of the SQLite EF Core database provider. ... AddTicks(value), datetime(@dateTime, (@value / 10000000.0) || ' seconds').
Read more >
SQLite datetime Function
In this tutorial, you will learn ho to use the SQLite datetime() function to manipulate datetime values.
Read more >
Linq query with the usage of the datetime .Ticks property ...
Hello, If I write a linq query such as the one that follows: var obj=(from tt in Session.Query () where tt.DateTime.AddTicks(10L).T.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found