﻿# 如何在 LINQPad 7 安裝 LINQPad.QueryPlanVisualizer 外掛 (Plugin)

我最近在研究如何讓 LINQPad 7 可以在每個查詢都使用 [LINQPad.QueryPlanVisualizer](https://github.com/Giorgi/LINQPad.QueryPlanVisualizer) 外掛來顯示 LINQ 實際執行的成效，顯示 SSMS 常見的執行計畫 (Query Execution Plan)。我發現在特定查詢安裝 NuGet 套件是可以使用的，但是依據官網的指示安裝成 Plugin 的話，就沒辦法正確使用，去 GitHub 提問也得不到什麼有效的答案，經過了好幾天的嘗試，我終於找出正確的設定方法，特別寫文章註記一下。

### 安裝 WebView2 Runtime 最新版

請事先安裝 [WebView2 Runtime](https://developer.microsoft.com/en-us/microsoft-edge/webview2/) 最新版，你可以透過下載 **Evergreen Bootstrapper** 進行安裝即可。

### 在特定查詢安裝 NuGet 套件來啟用此功能

1.  透過 **Query > NuGet Package Manager** 加入 `LINQPadQueryPlanVisualizer` 套件
    
2.  透過 **Query > Namespace Imports** 並加入 `ExecutionPlanVisualizer` 命名空間，再按 `OK` 關閉視窗
    
3.  連接 `ContosoUniversity` 資料庫，並執行 `Courses.Take(100).DumpPlan()` (Expression) 即可看到結果！
    

### 安裝成 Plugin 並在所有 Queries 預設啟用

1.  下載 [latest release](https://github.com/Giorgi/QueryPlanVisualizer/releases/latest) 並解壓縮到 `My Documents\LINQPad Plugins\NetCore3` 目錄
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/462f24bc3513352cdb6a-153029980-d007ee76-e6f0-42f7-a6f4-40deeac342ac.png)
    
2.  開啟 LINQPad 7 應用程式
    
3.  執行 **File > New Query** 建立新查詢，並透過連線視窗執行 EF 查詢
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/cc285aa5dcbaeb4a2427-153617422-e91015ea-85b3-4b24-a621-0f00385ddef8.png)
    
4.  執行 **Query > Namespace Imports** 設定兩個頁籤的資訊
    
    切換到 **Additional References** 頁籤，點擊 **Add NuGet** 按鈕，並加入 `ExecutionPlanVisualizer` 這個 NuGet 套件
    
    ![Additional References > Add NuGet](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/89063f630467713fa788-153618226-bc2cf858-1297-4b5a-9079-f9e2c133132d.png)
    
    切換到 **Namespace Imports** 命名空間，加入 `ExecutionPlanVisualizer` 命名空間，然後按下 `Set as default for new queries` 按鈕，再按 `OK` 關閉視窗即可設定完成！
    
    ![Namespace Imports > Set as default for new queries](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/358933dc9aa2feab4842-153618602-ac959798-2cbf-4aba-a4ca-1d3ebfe37ecc.png)
    
    > 這裡設定的關鍵，就是要同時設定好 NuGet 與 Namespace 後，才能按下 `Set as default for new queries` 按鈕，之後的新查詢才會預設載入 NuGet 與 Namespace，之後就不用再設定相同步驟了！
    
5.  修改查詢的語法，將 `Courses.Take (100)` 修改為 `Courses.Take (100).DumpPlan()` 即可看到結果！
    
    ![Courses.Take (100).DumpPlan()](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/2a0f891f4a961e8b8ab4-153619017-3f50e046-0d18-4828-b3bb-b0b53921fc71.png)
    

### 相關連結

-   [Giorgi/LINQPad.QueryPlanVisualizer: SQL Server and PostgreSQL query execution plan visualizer for LINQPad](https://github.com/Giorgi/LINQPad.QueryPlanVisualizer)
