Using LINQ in NCache
No code change is required for using NCache with LINQ except that a new assembly reference and namespace should be added in application. Rest of the code will work without any change in application.
To utilize the API, include the following namespace in your application:
Alachisoft.NCache.Linq
found in%NCHOME%\integrations\LINQToNCache.
The following code shows how to implement the NCacheQuery
class which further
implements IQueryable
interface. The code also shows the LINQ query based on
NCache Query Language running on a cached data.
//create your custom class LINQ object by giving handle of your cache.
IQueryable<Product> products = new NCacheQuery<Product>(cache);
try{
var result = from product in products
where product.ProductID > 1010
select product;
if (result != null){
foreach (Product product in result){
//put your code here for modification of products.
}
}
else{
//if there is no result in the cache related to your query
}
}
catch (Exception{
//can be operation failed due to server lost, state transfer or invalid query format of LINQ
}