Using Wildcard Operators
NCache supports multiple wildcard operators with LINQ.
Pre-Requisites
For flawless program delivery, ensure the following:
- Include the following namespaces in your application:
AlalchiSoft.NCache.Web.Caching
Alachisoft.NCache.Linq
(located at %NCHOME%\integrations\LINQToNCache)
- Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
- 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 code uses the NCacheQuery
class with two of the Wildcard operators 'StartsWith' and 'EndsWith' for determining products in a given range of values. It is assumed the cache contains Product objects.
Wildcard 'StartsWith':
A Product class LINQ object is created which returns products with names that start with "Bev".
Using Simple LINQ Query Expression:
try
{
// Precondition: Cache is already connected
// Create custom class LINQ object
IQueryable<Product> products = new NCacheQuery<Product>(cache);
// LINQ query to search cache
var result = from product in products
where product.ProductName.StartsWith("Bev")
select product;
if (result != null)
{
foreach (Product product in result)
{
// Perform operations
}
}
else
{
// No result in the cache related to query
}
}
catch (OperationFailedException ex)
{
//NCache specific exception
if (ex.Message.Contains("Index is not defined."))
{
// Index is not defined for the object queried.
// Index all objects using NCache Manager or Config files.
}
else
{
//Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Using Lambda Expression:
try
{
// Precondition: Cache is already connected
// Create custom class LINQ object
IQueryable<Product> products = new NCacheQuery<Product>(cache);
// Lambda expression for same query
var result = products.Select(p => p).Where(p => p.ProductName.StartsWith("Be"));
if (result != null)
{
foreach (Product product in result)
{
// Perform operations
}
}
else
{
// No result in the cache related to query
}
}
catch (OperationFailedException ex)
{
//NCache specific exception
if (ex.Message.Contains("Index is not defined."))
{
// Index is not defined for the object queried.
// Index all objects using NCache Manager or Config files.
}
else
{
//Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Wildcard Operator 'EndsWith':
A Product class LINQ object is created which returns products with names that end with "ages".
Using Simple LINQ Query Expression:
try
{
// Precondition: Cache is already connected
// Create custom class LINQ object
IQueryable<Product> products = new NCacheQuery<Product>(cache);
// LINQ query to search cache
var result = from product in products where product.ProductName.EndsWith("ages")
select product;
if (result != null)
{
foreach (Product product in result)
{
// Perform operations
}
}
else
{
// No result in the cache related to query
}
}
catch (OperationFailedException ex)
{
//NCache specific exception
if (ex.Message.Contains("Index is not defined."))
{
// Index is not defined for the object queried.
// Index all objects using NCache Manager or Config files.
}
else
{
//Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Using Lambda Expression:
try
{
// Precondition: Cache is already connected
// Create custom class LINQ object
IQueryable<Product> products = new NCacheQuery<Product>(cache);
// Lambda expression for same query
var result = products.Select(p => p).Where(p =>
p.ProductName.EndsWith("ages"));
if (result != null)
{
// Perform operations
}
else
{
// No result in the cache related to query
}
}
catch (OperationFailedException ex)
{
//NCache specific exception
if (ex.Message.Contains("Index is not defined."))
{
// Index is not defined for the object queried.
// Index all objects using NCache Manager or Config files.
}
else
{
//Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Additional Resources
NCache provides sample application for using LINQ with NCache at:
- GitHub
- Shipped with NCache: %NCHOME%\samples\dotnet\NCacheLINQ
See Also
Search Objects with LINQ Query
Using Basic Query Operators
Using Logical Operators
Using Aggregate Operators