Skip to content

Commit e6ea2e5

Browse files
committed
Fix type casts
1 parent 70711d6 commit e6ea2e5

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

SQLiteSharp.Tests/ConvertTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace SQLiteSharp.Tests;
2+
3+
public class ConvertTest {
4+
[Fact]
5+
public void Test1() {
6+
// Open a database connection
7+
using SqliteConnection Connection = new(":memory:");
8+
9+
// Create a table for a class
10+
SqliteTable<GameEnemy> GameEnemies = Connection.GetTable<GameEnemy>("GameEnemies");
11+
12+
// Insert items into the table
13+
GameEnemies.Insert(new GameEnemy() {
14+
Type = GameEnemyType.Zombie,
15+
});
16+
17+
// Find one item in the table matching a predicate
18+
GameEnemies.FindOne(GameEnemy => GameEnemy.Type == GameEnemyType.Zombie).ShouldNotBeNull();
19+
}
20+
}
21+
22+
public class GameEnemy {
23+
public GameEnemyType Type { get; set; }
24+
}
25+
26+
public enum GameEnemyType {
27+
Slime,
28+
Zombie,
29+
Skeleton,
30+
}

SQLiteSharp/SqlBuilder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ public string ExpressionToSql(Expression expression, ParameterExpression rowExpr
403403

404404
// Unary (!a)
405405
case UnaryExpression unaryExpression:
406+
if (unaryExpression.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked) {
407+
// Casts not necessary with SQLite's dynamic typing
408+
return ExpressionToSql(unaryExpression.Operand, rowExpression);
409+
}
406410
return $"({OperatorToSql(unaryExpression.NodeType)} {ExpressionToSql(unaryExpression.Operand, rowExpression)})";
407411

408412
// Binary (a == b)

0 commit comments

Comments
 (0)