Querying by Property
Index a property in the ScaleOut service using the [SossIndex] attribute. Indexed properties can then be used in a LINQ Where clause.
Procedure
Add a
using Scaleout.Client.QuerySupport
directive to the top of a C# source file to gain access to query extension methods and attributes.Mark a property with the [SossIndex] attribute to index it in the ScaleOut service.
- Multiple properties in a class can be indexed.
- Pass HighPriorityHashable into the attribute's constructor to improve query performance. Up to 8 properties in a class can be added to the service's hashed indexes.
class UserInfo { public int UserId { get; set; } [SossIndex(HashIndexPriority.HighPriorityHashable)] public string ZipCode { get; set; } }
Indexes in the ScaleOut service are updated when objects are added or updated in a cache.
var conn = GridConnection.Connect("bootstrapGateways=localhost:721"); var cache = new CacheBuilder<int, UserInfo>("user cache", conn).Build(); // Add users cache.Add(key: 1, new UserInfo { UserId = 1, ZipCode = "12345" }); cache.Add(key: 2, new UserInfo { UserId = 2, ZipCode = "54321" });
Use the QueryObjects() method to create a LINQ expression and query the ScaleOut Service.
var queryResult = cache.QueryObjects() .Where(user => user.ZipCode == "54321"); foreach (var user in queryResult) Console.WriteLine(user.ZipCode); // Output: 54321
If only the cache keys need to be retrieved from the query, use the
SelectKeys
operator to return the object's key instead of its value in the cache.var justKeys = cache.QueryObjects() .Where(user => user.ZipCode == "12345") .SelectKeys(cache); foreach (var cacheKey in justKeys) Console.WriteLine(cacheKey); // Output: 1