views:

20

answers:

1

How do I force LINQPad to give me a non-cached result from a LINQ2SQL query?

The scenario is this:

1) examine some data (from a in MyThings where ... select a).First(); 2) modify data outside LINQPad immediately after (service call) 3) re-examine some data (from a in MyThings where ... select a).First();

It seems to be caching results. :-/

+1  A: 

LINQPad instantiates a fresh DataContext each time you hit F5, so there's no chance of caching between query runs. However, if you dump twice in the same query, the second result will cache:

Customers.First (c => c.Name == "John").Dump();
Thread.Sleep (5000);
Customers.First (c => c.Name == "John").Dump();   // Result will be cached

This is by virtue of LINQ to SQL's object tracking. You can disable it by switching object tracking off - just as you would ordinarily:

ObjectTrackingEnabled = false;
Customers.First (c => c.Name == "Tom").Dump();
Thread.Sleep (5000);
Customers.First (c => c.Name == "Tom").Dump();   // Result will not be cached
Joe Albahari
Forgot to say: thanks! :-)
Bent Rasmussen