Skip to content

Commit

Permalink
Add String.Replace handling in linq queries
Browse files Browse the repository at this point in the history
Fixes #460
  • Loading branch information
praeclarum committed Aug 12, 2017
1 parent 44f2c5f commit 689ee77
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/SQLite.cs
Expand Up @@ -3333,6 +3333,9 @@ private CompileResult CompileExpr (Expression expr, List<object> queryArgs)
else if (call.Method.Name == "ToUpper") {
sqlCall = "(upper(" + obj.CommandText + "))";
}
else if (call.Method.Name == "Replace" && args.Length == 2) {
sqlCall = "(replace(" + obj.CommandText + "," + args[0].CommandText + "," + args[1].CommandText + "))";
}
else {
sqlCall = call.Method.Name.ToLower () + "(" + string.Join (",", args.Select (a => a.CommandText).ToArray ()) + ")";
}
Expand Down
22 changes: 22 additions & 0 deletions tests/LinqTest.cs
Expand Up @@ -294,5 +294,27 @@ public void CastedParameters ()

Assert.AreEqual ("Foo", r.Value);
}

[Test]
public void Issue460_ReplaceWith2Args ()
{
var db = CreateDb ();
db.Trace = true;
db.Tracer = Console.WriteLine;

db.Insert (new Product {
Name = "I am not B X B",
});
db.Insert (new Product {
Name = "I am B O B",
});

var cl = (from c in db.Table<Product> ()
where c.Name.Replace (" ", "").Contains ("BOB")
select c).FirstOrDefault ();

Assert.AreEqual (2, cl.Id);
Assert.AreEqual ("I am B O B", cl.Name);
}
}
}

0 comments on commit 689ee77

Please sign in to comment.