Use LINQ Queries for Objects
NCache integrates with the Language Integrated Query (LINQ) provider to enable developers to search distributed cached data using standard .NET LINQ expressions. It is integrated using the NCacheQuery class, which implements the IQueryable interface provided by the .NET framework. This allows for seamless object querying within a distributed cache using familiar LINQ syntax.
To use LINQ with NCache, initialize the cache and create an NCacheQuery<T> instance for the type you want to query. Existing LINQ query expressions can then be executed against this queryable object.
LINQ Queries: Querying Objects through NCacheQuery class
You can use any of the following supported LINQ operators to retrieve NCache objects using NCacheQuery:
- Projection Operators: Transform data using operators like
Select. - Restriction Operators: Filter data using criteria like
Where. - Basic Query Operators: Compare values using operators such as
==,!=,<,>,<=, and>=. - Logical Operators: Combine multiple criteria using
&& (AND)and|| (OR). - Aggregation Operators: Perform calculations like
Sum,Count,Average,Max, andMinon the server-side. - Wildcard Query Operators: Perform string matching using
StartsWith,EndsWith,Contains, andEquals.
Prerequisites to Use LINQ Queries
Before using LINQ queries, ensure that following prerequisites are fulfilled:
- Install the following NuGet packages in your .NET client application:
- Enterprise: Linq.NCache
- Include the following namespaces in your application:
- Indexing for searchable objects and their attributes needs to be configured first as explained in our section on Dynamic Indexing and Configuring Query Indexes in the Administrator's Guide.
- The cache must be running.
- For API details, refer to: NCacheQuery.
LINQ Operations
The following code uses the NCacheQuery class to demonstrate several operators we've discussed and illustrate how to use NCache with LINQ.
Using 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
IQueryable<Product> result = from prod in products where (prod.Category == "Beverages" || prod.Category == "Dairy") && prod.UnitsInStock > 10 select prod;
if (result != null)
{
foreach (Product product in result)
{
Console.WriteLine($"Product '{product.ProductName}' in Category '{product.Category}' has stock of '{product.UnitsInStock}' units");
}
}
else
{
Console.WriteLine($"No product found");
}
}
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
}
Using Lambda Expression
// Lambda expression for same query
IQueryable<Product> result = products.Select(prod => prod).Where(prod => ((prod.Category == "Beverages" || prod.Category == "Dairy") && prod.UnitsInStock > 10));
Note
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 NCache LINQ on GitHub.
See Also
.NET: Alachisoft.NCache.Linq namespace.