在Linqpad中重新运行的查询之间保存了什么状态? [英] What state is saved between rerunning queries in Linqpad?

查看:99
本文介绍了在Linqpad中重新运行的查询之间保存了什么状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linqpad中重新运行的查询之间保存了什么状态?我猜没有,所以如果您两次运行一个脚本,两次结果都相同.

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.

但是,在同一Linqpad选项卡中,两次在下面运行C#程序.您会发现第一个打印一个空列表,第二个显示带有消息嘿"的列表.发生了什么事?

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"));

推荐答案

LINQPad会在两次查询之间缓存应用程序域,除非您在Edit |首选项(或按Ctrl + Shift + F5清除应用程序域).这意味着假设类型在数值上相同,则存储在静态变量中的所有内容将在查询之间保留.这就是为什么您在代码中看到其他类型描述属性的原因,并且还说明了为什么经常在后续查询运行中看到性能优势的原因(因为许多内容在静态变量中以一种方式或另一种方式进行缓存).

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).

您可以通过LINQPad的Cache扩展方法来显式利用此优势:

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

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

Cache()是一个透明的扩展方法,如果在先前的查询中尚未看到输入,则将完全返回其输入的内容.否则,它将返回上一个查询的枚举结果.

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.

这篇关于在Linqpad中重新运行的查询之间保存了什么状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆