Query Cache with Language Integrated Query (LINQ) Overview
LINQ or Language Integrated Query, is a Microsoft .NET component that adds data querying features to .NET languages. This language is an integrated feature of the primary .NET programming language, such as Visual C# and Visual Basic. LINQ is a generic query language that allows one to make queries and filter out information from multiple data sources, such as relational databases, XML, ADO.NET DataSet, etc. Syntax-wise, LINQ is somewhat similar to SQL. However, what sets LINQ apart is its ability to allow query expressions to be more efficient while catering to syntax checking during code compilation, IntelliSense, and more. Moreover, with Language Integrated Query (LINQ), users can avail the benefit of not only querying information from external sources but also in-memory information.
By combining LINQ with NCache, you can add to the existing advantages of NCache with increased application performance. Further, LINQ can be integrated seamlessly with NCache to query information within NCache via a LINQ provider. This provider facilitates the execution of LINQ queries over the distributed cache while maintaining the improved application performance - without changing the LINQ object model.
In essence, the NCache LINQ provider converts LINQ-related query into NCache's extended SQL format and returns the results accordingly after transforming it into LINQ format. In addition, NCache supports two types of syntax for writing a Language Integrated Query query: the Simple LINQ Query Expression or Lambda Expression. For example, to query the cache for a product with ProductID = 1001, the query can be executed as follows:
- Simple LINQ Query Expression
var result = from p in products
where p.ProductID == 1001
select p;
- Lambda Expression
var result = products.Select(p => p).Where(p => p.ProductID == 1001);
When to Use Language Integrated Query?
A typical use case for employing Language Integrated Query with NCache would be to query an array of products in your cache. You may require specific item queries in a predefined array of products in the memory. If you use SQL to fetch the data each time it is needed, it will incur extra network hops for each call resulting in increased cost and delay in application performance.
Using LINQ you can query for and retrieve the required data already stored in memory. All this is done via a class implementing the IQueryable interface within the NCacheQuery class, this is how NCache communicates with LINQ. It sends the request for the data, which is translated and executed through LINQ and returned to the user. Users can query for any required value against a product item (such as product ID or product name), perform numeric manipulation (such as counting the number of items within a given range), and perform aggregate functions. All this occurs without any effect on the application and the data contained.
Defining Indexes
To perform queries smoothly, NCache requires all searchable attributes to be indexed. NCache provides its indexing mechanism through which the users can identify objects in .NET assemblies that they want to index - programmatically and through the NCache Management Center. For more information regarding Query Indexing, see the section Indexing and Define Indexes Programmatically for details.
Querying Objects through NCacheQuery class
You can use any of the following supported LINQ operators to retrieve NCache objects using NCacheQuery
:
- Projection Operators
- Restriction Operators
- Basic Query Operators
- Logical Operators
- Aggregation Operators
- Wildcard Query Operators
See Also
Configure LINQPad for NCache
Query Data in Cache with LINQPad