Using GROUP BY
NCache provides you with the ability to group the data according to the given criteria using GROUP BY clause.
Important
Note that the GROUP BY
clause cannot be used without an Aggregate function.
Pre-Requisites
- Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
- Include the following namespace in your application:
Alachisoft.NCache.Web.Caching
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
The following example groups the products from the cache based on the category using ExecuteReader. It retrieves all the products with unitPrice greater than 5 and groups them according to their category. For example, the products with unitPrice greater than 5 belonging to the category Beverages will be retrieved and grouped. Similarly data will be grouped based on other categories.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Specify the query with the specified criteria
// Use the fully qualified name of your own custom class
string query = "SELECT COUNT(Alachisoft.NCache.Sample.Data.Product) WHERE this.UnitPrice > ? GROUP BY this.Category";
// Add the values in the hashtable for executing query
// The query will get all the items with UnitPrice > 5
// And group them according to their category
Hashtable values = new Hashtable();
values.Add("UnitPrice", 5);
// Execute query using ExecuteReader
ICacheReader reader = cache.ExecuteReader(query, values);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
object result = reader.GetValue(0);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Product object
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.Message.Contains("Incorrect query format"))
{
// Make sure that the query format is correct
}
else if (ex.Message.Contains("Index is not defined"))
{
// Make sure that index is defined using Query Indexes through NCache Manager
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides sample application for SQL Searching at:
Shipped with NCache: %NCHOME%\samples\dotnet\ObjectQueryLanguage
See Also
Using Locks for Concurrent Updates
SQL Search for Objects
SQL Search for Keys
Using IN Operator
Using Like Operator
Query Operators
Search Cache with LINQ