Searching on String Fields
Marten supports a subset of the common sub/string searches:
public void string_fields(IDocumentSession session)
{
session.Query<Target>().Where(x => x.String.StartsWith("A"));
session.Query<Target>().Where(x => x.String.EndsWith("Suffix"));
session.Query<Target>().Where(x => x.String.Contains("something"));
session.Query<Target>().Where(x => x.String.Equals("The same thing"));
}
Marten also supports case insensitive substring searches:
public void case_insensitive_string_fields(IDocumentSession session)
{
session.Query<Target>().Where(x => x.String.StartsWith("A", StringComparison.OrdinalIgnoreCase));
session.Query<Target>().Where(x => x.String.EndsWith("SuFfiX", StringComparison.OrdinalIgnoreCase));
// using Marten.Util
session.Query<Target>().Where(x => x.String.Contains("soMeThiNg", StringComparison.OrdinalIgnoreCase));
session.Query<Target>().Where(x => x.String.Equals("ThE SaMe ThInG", StringComparison.OrdinalIgnoreCase));
}
A shorthand for case-insensitive string matching is provided through EqualsIgnoreCase
(string extension method in Baseline):
query.Query<User>().Single(x => x.UserName.EqualsIgnoreCase("abc")).Id.ShouldBe(user1.Id);
query.Query<User>().Single(x => x.UserName.EqualsIgnoreCase("aBc")).Id.ShouldBe(user1.Id);
This defaults to String.Equals
with StringComparison.CurrentCultureIgnoreCase
as comparison type.