LinqToDb produces a query that wraps Int64 DateTime.Ticks value in SQLite DateTime() function?
See original GitHub issueHello, 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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Looking into DateTime conversion and inlining problem. Looks like there are legacy conversions.
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)