{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 使用 扩展库 Microsoft.DotNet.Interactive.ExtensionLab" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 添加 Nuget 包, 成功后有用法摘要提示" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "// 可以先添加包源\n", "#i \"https://api.nuget.org/v3/index.json\"\n", "\n", "//添加 Nuget 包\n", "#r \"nuget: Microsoft.DotNet.Interactive.ExtensionLab,*-*\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 使用 #!linqify 创建强类型的 数据帧" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "#!linqify magic命令将 Microsoft.Data.Analysis.DataFrame 变量替换为生成的强类型数据帧,从而允许对包含的数据使用LINQ操作。" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!linqify frame --show-code\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## ExplainCode" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "这个扩展使用 Mermaid 内核从 csharp 代码生成序列图" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 使用 #!inspect 命令,检查代码编译详细信息!" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "#!inspect 命令,允许您查看C#单元格代码的反编译、IL和JIT Asm. 注意:深色模式下,展示区顶部的Tab[C# | IL JIT | ASM]不明显,但可以正常点击切换,或者换个VSCode主题。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!inspect\n", "foreach(var x in Enumerable.Range(1,10))\n", "{\n", " Console.WriteLine(x);\n", "}" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 使用 #!record命令来保存您运行的代码的副本" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "一旦您使用 #!recore 启用转录本记录,每个代码提交(包括重新运行的单元格)都记录在指定的文件中。所使用的JSON格式与.NET Interactive stdio和http API识别的格式相同,可用于通过自动化回放交互式会话。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!record --output recore.json\n", "//会在笔记所在的目录生成 recore.json 文件" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "Console.WriteLine(\"#!record\");" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 查询 SQLite 数据库" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "此扩展添加了对使用 #!connect sqlite 命令连接到SQLite数据库的支持!有关详细信息,请使用 #!sql 魔术命令。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "sql" }, "polyglot_notebook": { "kernelName": "sql" } }, "outputs": [], "source": [ "#!connect sqlite --kernel-name MySQLiteDemo \"Data Source=SQLiteDemo.db\"\n", "\n", "/*\n", " 相对目录位置\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=Database\\SQLiteDemo.db;\"\n", " \n", " 缓存共享\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=Database\\SQLiteDemo.db;Cache=Shared;\"\n", "\n", " 使用带密码\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=SQLiteDemo.db;Cache=Shared;Password=MyEncryptionKey;\"\n", " \n", " 只读模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=SQLiteDemo.db;Mode=ReadOnly\"\n", "\n", " 读写创建模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=SQLiteDemo.db;Mode=ReadWriteCreate\"\n", "\n", " 读写模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=SQLiteDemo.db;Mode=ReadWrite\"\n", "\n", " 私有内存模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=:memory:\"\n", "\n", " 共享内存模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=Sharable;Mode=Memory;Cache=Shared\"\n", "*/" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "dotnet_interactive": { "language": "sql-MySQLiteDemo" }, "polyglot_notebook": { "kernelName": "sql-MySQLiteDemo" } }, "outputs": [ { "data": { "text/csv": [ "Id,Name,Age\r\n", "1,张三,66\r\n", "2,李四,55\r\n" ], "text/html": [ "
IdNameAge
1
张三
66
2
李四
55
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#!sql-MySQLiteDemo\n", "\n", "select * from person;" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "name": "polyglot-notebook" }, "orig_nbformat": 4, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [], "name": "csharp" }, { "aliases": [], "name": "razor" } ] } } }, "nbformat": 4, "nbformat_minor": 2 }