Running queries on already fetched data from database in Linqpad (cache needed)

Viewed 409

I execute a long running query in Linqpad to fetch some data from my database. Then I need to analyze this data. The issue is that I don't want to fetch the data every time when I change my analyze queries.

What would be easiest way to store the data somewhere like a cache and still have Linq functionalities for analysis? Does LinqPad provides anything for that purpose? I know it can be exported as csv or xml file, but need something more convenient.

1 Answers

LINQPad provides a Cache extension method and a Util.Cache method for caching data in memory between runs. The Cache extension method works on IEnumerable<T> whereas the Util.Cache method works on any type.

Here's how to use the Cache extension method:

var customers = Customers.Cache();              // Slow only on the first execution
customers.Where (c => c.Name == "Joe").Dump();

After running the query, change the second line to something like this:

customers.Where (c => c.Name == "Mary").Dump();

When you re-execute the query, LINQPad will not fetch anything from the database because customers will have been cached in memory from the first run.

You can also cache a subset of the data:

var localCustomers = Customers.Where (c => c.Country == "US").Cache();
localCustomers.Where (c => c.Name == "Mary").Dump();

There are more examples in LINQPad's built-in tutorial and reference. Press Ctrl+, to bring up the search dialog, click Search Samples and search for 'Caching'.

Should you need to, you can clear the cache by killing the process via the option on the query menu (Ctrl+Shift+F5).

Related