In real life complex scenarios, your application often needs to fetch data based on certain attributes rather than searching through the primary key. Querying your data using SQL or LINQ makes it easier for you to retrieve data from your database depending upon your custom logic at runtime.
NCache supports SQL and LINQ querying capabilities that let you query your distributed cache the exact way you query your database. For this reason, NCache provides various projections, operations and aggregate functions in SQL and LINQ support to reduce network traffic and enhance performance.
NCache supports SQL querying mechanism to let you search your cache according to a given criterion and returns the required result set. NCache uses a query language that is very close to the native SQL structured language, which makes it easy for you to query your indexed cached data.
NCache uses query interface similar to the ADO.NET interface to support various query methods to search for or delete data from the cache. Here are some of the most important query methods supported in NCache.
string query = "SELECT * FROM FQN.Product WHERE UnitsInStock > ?";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("UnitsInStock",0);
ICacheReader reader = cache.ExecuteReader(queryCommand);
while (reader.Read())
{
string ID = reader.GetValue<string>("ProductID");
string Name = reader.GetValue<string>("ProductName");
}
NCache allows you to specify columns of your choice to be projected against your SQL query. Mentioned below are the basic, most commonly used projections supported in NCache,
string query = "SELECT COUNT(*) FROM FQN.Product WHERE UnitsInStock > ?";
Refer to SQL Search for Object for more information.
These operators are used and re-used throughout SQL database for processing and manipulating data. NCache supports various SQL operators that allow you to get the desired result efficiently. These functions are:
string query = @"SELECT * FROM FQN.Product WHERE UnitsInStock < ?
Group By Category
ORDER BY UnitsInStock DESC";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("UnitsInStock",50);
SQL operators are reserved keywords in the WHERE clause to perform logical and comparison operations. NCache provides you support of the following SQL operators to query your data according to custom conditions.
string query = "SELECT ProductName, UnitsAvailable From FQN.Product WHERE Category IN (‘Beverages’, ‘Confections’)";
Similarly, you have the NOT LIKE operator.
string query = "SELECT * FROM FQN.Product WHERE ProductName LIKE ? AND Category LIKE ?";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("ProductName", "Cha*");
queryCommand.Parameters.Add("Category", "?dib*");
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
To get acquainted with how to query cache data, refer to our documentation on SQL Search in Cache.
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 in functionalities, it provides you with better and more optimized querying methods when querying your NCache servers. It provides 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 execution of LINQ queries over the distributed cache while improving the application’s performance without changing the LINQ object model.
NCache supports LINQ language’s basic querying expressions to search cache data. They are:
IQueryable<Product> products = new NCacheQuery<Product>(cache);
IQueryable<Product> product = products.Select(p => p).Where(p => p. UnitsInStock > 10);
IQueryable<Product> products = new NCacheQuery<Product>(cache);
IQueryable<Product> product = from prod in products
where prod.UnitsInStock > 10
select prod;
LINQ operators are extension methods that offer query capabilities like projection, aggregation, filtering, and sorting. Among these operators, NCache supports the ones mentioned below:
IQueryable<Product> product = products.Select(prod => prod);
IQueryable<Product> product = products.Select(prod => prod).Where(prod => prod.UnitsInStock > 10);
NCache supports basic query operators with LINQ querying like:
Refer to LINQ Query Operators for the usage of these operators.
NCache supports two logical operators with LINQ expressions; AND (&&) operator and OR (||) operator. These operators are used to combine two or more restriction criteria for the cache search. Follow our guide on LINQ Logical Operators to understand their usage in detail.
NCache supports aggregation operators to allow any type of desired aggregation in LINQ. The operators NCache supports are:
An example of how to use aggregate functions to search for cache data and count the results is shown below:
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.
In conjunction with the restriction and aggregation operators, NCache supports multiple wildcards in LINQ expressions. These wildcard operators are:
An example of using wildcards with a restriction where operator to search for a specific data set in the cache is shown below:
IQueryable<Product> product = products.Select(p => p).Where(p => p.ProductName.StartsWith("Be"));
Refer to our documentation on LINQ Wildcard Operators to know more.
LINQPad is a third party tool that is used to interactively query SQL database using query and lambda expressions as well as write C# codes without any IDE. NCache supports full 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 that is an implementation of .NET framework’s IQueryable interface.
IQueryable<Product> product = new NCacheContext<Product>("myPartitionedCache");
IQueryable<Product> products = from prod in product
where prod.UnitsInStock > 50
select prod;
products.Dump();
Running this script in the LINQPad will create a LINQ object product which is used to query the products with UnitsInStock greater than 50.
To know more on 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.