Using Logical Query Operators
You can also use logical operators AND, OR, NOT in your query. These operators are used for specifying more than one condition after the operator. Whereas the NOT operator displays the result if the condition is not true.
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 shows how to use the combination of AND and OR operator in a single query within the WHERE clause using ExecuteReader. The following query retrieves all the products which brlong to the category Beverages and have UnitsInStock greater than 0 or all the products which belong to the category Produce having UnitPrice greater than 100.
try
{
// Pre-condition: Cache is already connected
// Create a query which will be executed on the data set
// Instead of Product, specify fully qualified name of your custom class
string query = "SELECT Alachisoft.NCache.Sample.Data.Product WHERE (this.Category = ? AND this.UnitsInStock > ?) OR (this.Category = ? AND this.UnitPrice > ?) ";
// Create a new hashtable
Hashtable values = new Hashtable();
// Create an array list of the categories since the WHERE clause
// has two category fields
// Categories will be Beverages and Produce respectively
ArrayList categoryList = new ArrayList();
categoryList.Add("Beverages");
categoryList.Add("Produce");
// Add the values of the all the attributes in the hastable
values.Add("Category", categoryList);
values.Add("UnitsInStock", 0);
values.Add("UnitPrice", 100);
// Query the cached Data
ICacheReader reader = cache.ExecuteReader(query, values);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Product result = (Product)reader.GetValue(1);
// 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
SQL Search for Objects
Using IN Operator
Using Like Operator
Using Group By
Query Operators
Search Cache with LINQ