LINQ Logical Operators Usage
Note
This feature is only available in NCache Enterprise Edition.
NCache supports two logical operators with LINQ.
Pre-Requisites
For flawless program delivery, ensure the following:
- Include the following namespaces in your application:
AlalchiSoft.NCache.Client
Alachisoft.NCache.Linq
(located at %NCHOME%\integrations\LINQToNCache)Alachisoft.NCache.Runtime.Exceptions
- 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.
- To handle any unseen exceptions, refer to the Troubleshooting section.
The following code uses the NCacheQuery
class with logical operators '&&' and '||'. It is assumed the cache contains Product objects.
Logical Operator '&&':
A Product class LINQ object is created which is used to query products which have ProductID
>= 1001 and ProductName
as "Chai".
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 prod in products
where prod.ProductID >= 1001 && prod.ProductName == "Chai"
select prod;
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
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
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(prod => prod).Where(prod => prod.ProductID >= 1001 && prod.ProductName == "Chai");
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
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Logical Operator '||':
A Product class LINQ object is created which is used to query product which has a ProductID
= 1001 or ProductName
as "Chai".
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 prod in products
where prod.ProductID == 1001 || prod.ProductName == "Chai"
select prod;
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
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
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(prod => prod).Where(prod => prod.ProductID == 1001 || prod.ProductName == "Chai");
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
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Additional Resources
NCache provides sample application for using LINQ with NCache on GitHub.
See Also
Use LINQ Query to Search for Objects
LINQ Basic Query Operators Usage
LINQ Aggregate Operators Usage
LINQ Wildcard Operators Usage