c# - 有没有办法保存 LINQPad.Util.Compile 的结果并在很长一段时间内重新运行它

标签 c# linq linqpad

我有一个脚本 Web 服务,可以从内容管理系统下载并运行 linqpad .linq 程序。我目前正在做类似下面代码的事情。有没有办法保存 LINQPad.Util.Compile 的结果并将其存储在某个地方,以便我可以使用它直到 .linq 文件发生更改?现在我认为它每次都在重新编译,并产生许多编译文件夹。

public static object DownloadAndRunScript(string scriptFilePath, object args)
{
     var compiledScript = LINQPad.Util.Compile(scriptFilePath, true);
     var queryExecutorResult = compiledScript.Run(LINQPad.QueryResultFormat.Text, args);
     return (object)queryExecutorResult.ReturnValue;
}

最佳答案

您可以很容易地为此编写一个缓存函数。像这样:

static Dictionary<string, Tuple<DateTime, QueryCompilation>> _cache
    = new Dictionary<string, System.Tuple<System.DateTime, QueryCompilation>>();

public static QueryCompilation GetCompilation (string scriptFilePath)
{
    Tuple<DateTime, LINQPad.ObjectModel.QueryCompilation> entry;
    var writeTime = new FileInfo (scriptFilePath).LastWriteTimeUtc;

    lock (_cache)
        if (_cache.TryGetValue (scriptFilePath, out entry) && entry.Item1 == writeTime)
            return entry.Item2;

    var compiledScript = LINQPad.Util.Compile (scriptFilePath, true);

    lock (_cache)
        _cache [scriptFilePath] = Tuple.Create (writeTime, compiledScript);

    return compiledScript;
}

关于c# - 有没有办法保存 LINQPad.Util.Compile 的结果并在很长一段时间内重新运行它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35203662/

相关文章:

c# - 如何在 A 列或 B 列上加入 Linq 查询

c# - 复杂查询的 Linq 内连接

c# - LINQPad - 将辅助 DataContext 记录到 "SQL"选项卡

c# - 集成 C# 和 R 问题

C# 3.0 (2009) 中的 C# 可选属性

c# - 在 XML 中添加没有 xml 元素的纯文本

c# - 如何将 Expression<Func<T, object>> 转换为 Expression<Func<object, object>>

c# - 如何用linq获取相关数据?

visual-studio-2010 - SQL Server 2000 的 LINQ-to-SQL "unsupported data provider"错误

c# - 如何将索引字段添加到 Linq 结果