Search code examples
c#hazelcast

Predicate usage in Hazelcast C# client


Java examples show the various usages of Predicate feature in Hazelcast but I'm unable to find a way to filter maps with C#.

For example this works in Java:

EntryObject e = new PredicateBuilder().getEntryObject();
Predicate agePredicate = e.get( "age" ).equal( age );
Predicate predicate = e.get( "name" ).equal( name ).and( agePredicate );
people = personMap.values( predicate );

Looking through the source code of C# client, I couldn't find a class named EntryObject or PredicateBuilder.

This is also another predicate object in Java client:

IMap<Employee> map = hazelcastInstance.getMap( "employee" );
Set<Employee> employees = map.values( new SqlPredicate( "active AND age < 30" ) );+

C# client has a SqlPredicate class but this code is not accepted in C#:

var predicate = new SqlPredicate("Type = 1 AND Enabled = 1");
var map = Cache.GetMap<int, Machine>(nameof(Machine));
var enabledMachines = map.Values(predicate);

Compiler error is:

CS1503: cannot convert from 'Hazelcast.Core.SqlPredicate' to 'Hazelcast.Core.IPredicate<int, Machine>'

which is expected by the way...

So what is the proper way to query maps in C#?


Solution

  • Using Hazelcast .NET client 3.6.2 you should be able to use the static Predicates class, which works similar to the Java one. The compile error that you are encountering should also be fixed. Previous versions of the .NET client had this issue.

    Some examples are available here:

    https://github.com/hazelcast/hazelcast-csharp-client/blob/master/src/Hazelcast.Net.Tests/Query/PredicatesTests.cs