• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

LINQ Aggregate Operators Usage

Note

This feature is only available in NCache Enterprise Edition.

NCache supports multiple aggregate operators with LINQ. This section covers the usage of aggregate operators in NCache with LINQ.

Prerequisites

  • Install the following NuGet package in your application:
    • Enterprise: Linq.NCache
  • Include the following namespaces in your application:
    • AlalchiSoft.NCache.Client
    • Alachisoft.NCache.Linq
    • 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.
  • Cache must be running.
  • The application must be connected to cache before performing the operation.
  • 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.

LINQ Aggregate Operators

The following code uses the NCacheQuery class uses two of the aggregate operators for determining 'count' and 'average' values in a given range of values. It is assumed the cache contains Product objects.

Aggregate Operator 'Count':

A Product class LINQ object is created which is used to count and return the number of products with ID <= 1001.

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 prod in products
                 where prod.ProductID <= 1001
                 select prod).Count();

    if (result > 0)
    {
            //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    
}

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(prod => prod.ProductID <= 1001).Count();

    if (result > 0)
    {
        //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
}

Aggregate Operator 'Average':

A Product class LINQ object is created which is used to return the average of the number of products with ID <= 1001.

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.ProductID < 1001
    select product.ProductID).Average(); 

    if (result > 0)
    {
        //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
}

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.Where(p => p.ProductID > 1001).Average(p => p.ProductID);

    if (result > 0)
    {
        //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 Logical Operators Usage
LINQ Aggregate Operators Usage

Back to top Copyright © 2017 Alachisoft