What state is saved between rerunning queries in Linqpad?

半城伤御伤魂 提交于 2020-02-28 04:04:51

问题


What state is saved between rerunning queries in Linqpad? I presumed none, so if you run a script twice it will have the same results both time.

However run the C# Program below twice in the same Linqpad tab. You'll find the first it prints an empty list, the second time a list with the message 'hey'. What's going on?


System.ComponentModel.TypeDescriptor.GetAttributes(typeof(String)).OfType<ObsoleteAttribute>().Dump();  
System.ComponentModel.TypeDescriptor.AddAttributes(typeof(String),new ObsoleteAttribute("hey"));

回答1:


LINQPad caches the application domain between queries, unless you request otherwise in Edit | Preferences (or press Ctrl+Shift+F5 to clear the app domain). This means that anything stored in static variables will be preserved between queries, assuming the types are numerically identical. This is why you're seeing the additional type description attribute in your code, and also explains why you often see a performance advantage on subsequent query runs (since many things are cached one way or another in static variables).

You can take advantage of this explicitly with LINQPad's Cache extension method:

var query = <someLongRunningQuery>.Cache();
query.Select (x => x.Name).Dump();

Cache() is a transparent extension method that returns exactly what it was fed if the input was not already seen in a previous query. Otherwise, it returns the enumerated result from the previous query.

Hence if you change the second line and re-execute the query, the query will execute quickly since will be supplied from a cache instead of having to re-execute.



来源:https://stackoverflow.com/questions/13010210/what-state-is-saved-between-rerunning-queries-in-linqpad

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!