Skip to content

Commit da0345a

Browse files
authored
Fix string.Contains with .NET 2.1 or higher (#3154)
1 parent 51a5c43 commit da0345a

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* Avoid calling the setter on UI-bound properties in case the new value of the property is the same as the current one. This avoids some issue with MAUI, that seems to be calling the setter of bound properties unnecessarily when CollectionView/ListView are shown on screen. This is problematic if the object does not belong to the current user's permissions, as it will cause a compensanting write. In some limited cases this could cause an error loop (verified on iOS) when recycling of cells is involved. (Issue [#3128](https://github.com/realm/realm-dotnet/issues/3128))
3636
* Fixes an issue with where the source generator will not add the namespace for types used in properties' initializers. (Issue [#3135](https://github.com/realm/realm-dotnet/issues/3135))
3737
* Fixed an issue that would prevent Realm from working correctly in Unity applications that have [Domain Reloading](https://docs.unity3d.com/Manual/DomainReloading.html) turned off. (Issue [#2898](https://github.com/realm/realm-dotnet/issues/2898))
38+
* Fixed a bug when using `string.Contains` in .NET 2.1 or later where the search string is not a literal. (Issue [#3134](https://github.com/realm/realm-dotnet/issues/3134))
3839

3940
### Compatibility
4041
* Realm Studio: 12.0.0 or later.

Realm/Realm/Linq/RealmResultsVisitor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,12 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
315315
}
316316
else if (IsStringContainsWithComparison(node.Method, out var index))
317317
{
318-
member = node.Arguments[0] as MemberExpression;
318+
if (index > 0)
319+
{
320+
// If index is > 0, we're an extension method where the 0th argument is the property name
321+
member = node.Arguments[0] as MemberExpression;
322+
}
323+
319324
stringArgumentIndex = index;
320325
queryMethod = (q, r, p, v) => q.StringContains(r, p, v, GetComparisonCaseSensitive(node));
321326
}

Tests/Realm.Tests/Database/RealmResults/SimpleLINQtests.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,16 +686,36 @@ public void StringSearch_Contains_CaseSensitivityTests()
686686
MakeThreePatricks();
687687

688688
// case sensitive
689-
var contains_atri = _realm.All<Person>().Where(p => p.FirstName.Contains("atri")).Count();
690-
Assert.That(contains_atri, Is.EqualTo(2));
689+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains("atri")).Count(), Is.EqualTo(2));
690+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains("atri")).ToArray().Length, Is.EqualTo(2));
691691

692692
// case sensitive
693-
var contains_ordinal_atri = _realm.All<Person>().Where(p => p.FirstName.Contains("atri", StringComparison.Ordinal)).Count();
694-
Assert.That(contains_ordinal_atri, Is.EqualTo(2));
693+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains("atri", StringComparison.Ordinal)).Count(), Is.EqualTo(2));
694+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains("atri", StringComparison.Ordinal)).ToArray().Length, Is.EqualTo(2));
695695

696696
// ignore case
697-
var contains_ignorecase_atri = _realm.All<Person>().Where(p => p.FirstName.Contains("atri", StringComparison.OrdinalIgnoreCase)).Count();
698-
Assert.That(contains_ignorecase_atri, Is.EqualTo(3));
697+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains("atri", StringComparison.OrdinalIgnoreCase)).Count(), Is.EqualTo(3));
698+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains("atri", StringComparison.OrdinalIgnoreCase)).ToArray().Length, Is.EqualTo(3));
699+
}
700+
701+
[Test]
702+
public void StringSearch_Contains_NonLiteral_CaseSensitivityTests()
703+
{
704+
MakeThreePatricks();
705+
706+
var searchString = "atri";
707+
708+
// case sensitive
709+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains(searchString)).Count(), Is.EqualTo(2));
710+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains(searchString)).ToArray().Length, Is.EqualTo(2));
711+
712+
// case sensitive
713+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains(searchString, StringComparison.Ordinal)).Count(), Is.EqualTo(2));
714+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains(searchString, StringComparison.Ordinal)).ToArray().Length, Is.EqualTo(2));
715+
716+
// ignore case
717+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains(searchString, StringComparison.OrdinalIgnoreCase)).Count(), Is.EqualTo(3));
718+
Assert.That(_realm.All<Person>().Where(p => p.FirstName.Contains(searchString, StringComparison.OrdinalIgnoreCase)).ToArray().Length, Is.EqualTo(3));
699719
}
700720

701721
[Test]

0 commit comments

Comments
 (0)