{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "dotnet-interactive.csharp" } }, "outputs": [ { "data": { "text/html": [ "
572
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
772
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "List numbers = new List(){ 1, 7, 18, 48, 28, 20, 17, 458 };\n", "IEnumerable evenNumbers = numbers.Skip(1).Where(n => n % 2 == 0);\n", "DisplayExtensions.Display(evenNumbers.Sum());\n", "numbers.Add(200);\n", "DisplayExtensions.Display(evenNumbers.Sum());" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "dotnet-interactive.csharp" } }, "outputs": [ { "data": { "text/html": [ "
indexvalue
0
4
1
36
2
144
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Func isEven = n => n % 2 == 0;\n", "DisplayExtensions.Display(\n", " new List(){3, 6, 6, 5, 2, 12, 11, 13}.Skip(2).Where(isEven).Select(n => n * n).OrderBy(n => n).ToArray());" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "dotnet-interactive.csharp" } }, "outputs": [ { "data": { "text/html": [ "
indexvalue
0
2
1
3
2
6
3
5
4
12
5
13
6
11
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "DisplayExtensions.Display(\n", " new List>(){ new List(){2, 3, 6, 5}, new List(){ 12, 13, 11 } }.SelectMany(l => l).ToArray());" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "dotnet-interactive.csharp" } }, "outputs": [ { "data": { "text/html": [ "
55
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
5
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "List numbers = new List(){3, 6, 2, 5, 6, 12, 11, 13};\n", "Func isEven = n => n % 2 == 0;\n", "DisplayExtensions.Display(\n", " numbers.Where(isEven)\n", " .Select(n => n * n)\n", " .Average());\n", "\n", "DisplayExtensions.Display(\n", " numbers.Where(i => i > 4)\n", " .OrderBy(i => i)\n", " .FirstOrDefault());" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "dotnet-interactive.csharp" } }, "outputs": [ { "data": { "text/html": [ "
indexvalue
0
6
1
2
2
6
3
12
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
indexvalue
0
[ 3, 5, 11, 13 ]
1
[ 6, 2, 12 ]
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
indexvalue
0
9
1
36
2
4
3
25
4
144
5
121
6
169
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "[3;6;2;5;6;12;11;13]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "List numbers = new List(){3, 6, 2, 5, 6, 12, 11, 13};\n", "Func isEven = n => n % 2 == 0;\n", "\n", "DisplayExtensions.Display(\n", " numbers.Where(isEven)\n", " .ToList());\n", "\n", "DisplayExtensions.Display(\n", " numbers.Distinct()\n", " .GroupBy(i => isEven(i) ? \"Even\" : \"Odd\"));\n", "\n", "DisplayExtensions.Display(\n", " numbers.Distinct().Select(i => i * i));\n", "\n", "DisplayExtensions.Display(\n", " $\"[{String.Join(\";\", numbers.ToArray())}]\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "dotnet-interactive.csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Filter: 3\r\n", "Filter: 2\r\n", "Map: 2 => 4\r\n", "Filter: 12\r\n", "Map: 12 => 144\r\n", "Reduce: aggregate=4 next=144 => 148\r\n", "Filter: 5\r\n", "Filter: 13\r\n", "Filter: 11\r\n" ] }, { "data": { "text/html": [ "
148
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "DisplayExtensions.Display(\n", " new List(){3, 2, 12, 5, 13, 11}.Select(\n", " (i) =>\n", " {\n", " Console.WriteLine($\"Filter: {i}\");\n", " return i;\n", " })\n", " .Where(i => i % 2 == 0)\n", " .Select(\n", " (i) =>\n", " {\n", " int result = i * i;\n", " Console.WriteLine($\"Map: {i} => {result}\");\n", " return result;\n", " })\n", " .Aggregate(\n", " (sum, nextValue) =>\n", " {\n", " int result = sum + nextValue;\n", " Console.WriteLine($\"Reduce: aggregate={sum} next={nextValue} => {result}\");\n", " return result;\n", " }));" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "dotnet-interactive.csharp" } }, "outputs": [ { "data": { "text/html": [ "
indexvalue
0This
1is
2test
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
indexvalue
0This
1is
2test
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
indexvalue
0This
1is
2test
3another1
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "List lines = new List(){\"This\", \"is\", \"a\", \"test\"};\n", "IEnumerable linesWithEvenCount = lines.Where(line => line.Length % 2 == 0);\n", "DisplayExtensions.Display(linesWithEvenCount);\n", "DisplayExtensions.Display(linesWithEvenCount.ToArray());\n", "lines.Add(\"another\");\n", "lines.Add(\"another1\");\n", "DisplayExtensions.Display(linesWithEvenCount.ToArray());" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "name": "C#" } }, "nbformat": 4, "nbformat_minor": 2 }