Cookie Consent by Free Privacy Policy Generator LINQ Support in NCache

LINQ Support in NCache

Real-world applications may frequently need to filter data rather than simply querying for the primary key. Using LINQ makes it easy to retrieve data from your database based on whatever custom logic you employ at runtime. Therefore, NCache supports LINQ querying, which allows you to query your distributed cache in the same manner you query your database.

LINQ is a generic .NET query language that allows you to search and filter out data from your data source. LINQ syntax is quite similar to SQL, but it provides you with better and more optimized querying methods when querying your NCache servers. It offers the ability to allow more efficient query expressions while catering to syntax checking during code compilation.

NCache seamlessly integrates LINQ to query information within the cache via a LINQ provider. The link provider facilitates the execution of LINQ queries over the distributed cache while improving the application's performance without modifying the LINQ object model.

Unlike standard LINQ-to-Objects which executes locally, NCache LINQ executes queries across the entire server cluster in parallel. This distributed execution ensures that filtering happens at the data source, significantly reducing the memory footprint on the client application.

 

Which LINQ Querying Expressions does NCache support?

NCache supports the LINQ language's basic querying expressions to search cache data. They are:

  • Lambda Expressions: Lambda expressions in a LINQ equation translate logic at runtime, so it can pass on the data source conveniently.
  • IQueryable<Product> products = new NCacheQuery<Product>(cache);
    IQueryable<Product> product = products.Select(p => p).Where(p => p. UnitsInStock > 10);
  • Query Expressions: Query expressions query and transform data from your LINQ-enabled distributed cache servers.
  • IQueryable<Product> products = new NCacheQuery<Product>(cache);
    IQueryable<Product> product = from prod in products
    	where prod.UnitsInStock > 10
    	select prod;
Feature Lambda Expressions (Fluent) Query Expressions (Declarative)
Syntax Style Method-based (Chained methods) SQL-like keywords (from, where, select)
Compilation Compiled directly into Expression Trees (for IQueryable) Lowered into Lambda method calls by the compiler, then turned into Expression Trees
NCache Execution Parsed into NCache SQL via NCacheQuery Parsed into NCache SQL via NCacheQuery
Best Use Case Better for dynamic query building/pipelining Better for complex joins or readable "flat" queries
 

LINQ Operators

LINQ operators are extension methods that offer query capabilities like projection, aggregation, filtering, and sorting. Among these operators, NCache supports the ones mentioned below:

  • Projection Operator: projects values based on a transform function. The following code snippet shows how to use the select projection operator to search cache data.
  • IQueryable<Product> product = products.Select(prod => prod);
  • Restriction Operator: restricts the result set to selected data that satisfies a particular condition. This code snippet searches the cache for all products whose units on order are greater than 10.
  • IQueryable<Product> product = products.Select(prod => prod).Where(prod => prod.UnitsInStock > 10);
 

Query Operators

NCache supports basic query operators with LINQ querying like:

  • Equals (==)
  • Not Equals (!=)
  • Less Than (<)
  • Greater Than (>)
  • Less Than Equal To (<=)
  • Greater Than Equal To (>=)

Refer to LINQ Query Operators for the usage of these operators.

 

Logical Operators

NCache supports two logical operators with LINQ - the AND (&&) and OR (||) operators. These operators combine two or more restriction criteria for the cache search. Follow our guide on LINQ Logical Operators to understand their usage in detail.

 

Aggregation Operators

NCache supports the following aggregation operators:

  • Count
  • Max
  • Min
  • Average
  • Sum

The following is an example of how to use aggregate functions to search for cache data and count the results:

int count = products.Select(p => p).Where(prod => prod.UnitsInStock <= 50).Count();

Understand the usage of aggregate operators in NCache from our guide on LINQ Aggregate Operators.

 

Wildcard Operators

NCache supports multiple wildcards in LINQ expressions. These wildcard operators are:

  • StartsWith
  • EndsWith
  • Contains
  • Equals

The following is an example of using wildcards with a restriction where the operator searches for a specific data set in the cache:

IQueryable<Product> product = products.Select(p => p).Where(p => p.ProductName.StartsWith("Be"));

Refer to our documentation on LINQ Wildcard Operators to know more.

 

How do I use LINQPad with NCache?

LINQPad is a third-party tool used to interactively query SQL databases using query and lambda expressions and write C# codes without any IDE. NCache supports seamless integration with the LINQPad utility to improve data analysis and application performance. To process queries in LINQPad, NCache LINQ Provider uses the NCacheContext class, which is an implementation of the .NET framework's IQueryable interface.

IQueryable<Product> product = new NCacheContext<Product>("ClusteredCache");
var result = from prod in product
	where prod.ProductID == 12
	select prod;

result.Dump();

Running this script in the LINQPad will create a LINQ object product to query the products with UnitsInStock greater than 50.

LINQPad 5 executing NCacheContext LINQ query to retrieve ProductID 12 from a distributed cache.
Querying NCache ClusteredCache with LINQPad.

To know more about how to use LINQPad to query data in cache, follow our documentation on Query Data in Cache with LINQPad, and to configure LINQPad in NCache for yourself, follow the steps provided in Configure LINQPad for NCache.

What to Do Next?

Frequently Asked Questions (FAQ)

NCache LINQ support allows you to query distributed cache data using standard C# syntax, executing filters in parallel across the server cluster to reduce client-side memory footprint and database load.

Yes, NCache supports Lambda expressions (fluent syntax) through the NCacheQuery class, enabling runtime logic translation for efficient cache searching.

NCache provides seamless integration with LINQPad via the NCacheContext class, allowing developers to interactively query clustered caches using C# statements without a full IDE.

NCache supports standard LINQ aggregation operators including Count, Max, Min, Average, and Sum for performing server-side calculations on cached data sets.

NCache supports multiple wildcard operators in LINQ expressions, specifically StartsWith, EndsWith, Contains, and Equals for flexible string-based filtering.

© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.