From a474b715fd45d586d3c1993092783b65f1404a58 Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 22 Aug 2023 10:52:21 +0900 Subject: [PATCH] linq --- .vscode/settings.json | 3 + .../MyConsoleApp/MyConsoleApp.csproj | 10 ++ .../MySolution/MyConsoleApp/Program.cs | 19 +++ .../MyConsoleApp/Work/AsyncAwaitWork.cs | 47 +++++ .../MySolution/MyConsoleApp/Work/LinqWork.cs | 160 ++++++++++++++++++ AdvancedC#Grammer/MySolution/MySolution.sln | 22 +++ HTML, CSS/test.html | 38 ++++- 7 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 AdvancedC#Grammer/MySolution/MyConsoleApp/MyConsoleApp.csproj create mode 100644 AdvancedC#Grammer/MySolution/MyConsoleApp/Program.cs create mode 100644 AdvancedC#Grammer/MySolution/MyConsoleApp/Work/AsyncAwaitWork.cs create mode 100644 AdvancedC#Grammer/MySolution/MyConsoleApp/Work/LinqWork.cs create mode 100644 AdvancedC#Grammer/MySolution/MySolution.sln diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f829cf2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.defaultSolution": "AdvancedC#Grammer/MySolution/MySolution.sln" +} diff --git a/AdvancedC#Grammer/MySolution/MyConsoleApp/MyConsoleApp.csproj b/AdvancedC#Grammer/MySolution/MyConsoleApp/MyConsoleApp.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/AdvancedC#Grammer/MySolution/MyConsoleApp/MyConsoleApp.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/AdvancedC#Grammer/MySolution/MyConsoleApp/Program.cs b/AdvancedC#Grammer/MySolution/MyConsoleApp/Program.cs new file mode 100644 index 0000000..0206426 --- /dev/null +++ b/AdvancedC#Grammer/MySolution/MyConsoleApp/Program.cs @@ -0,0 +1,19 @@ +using MyConsoleApp.Work; + +namespace MyConsoleApp; +class Program +{ + + + static void Main(string[] args) + { + //AsyncAwaitWork.DoTest(); + + LinqWork.DoTest(); + + while (true) + { + + } + } +} diff --git a/AdvancedC#Grammer/MySolution/MyConsoleApp/Work/AsyncAwaitWork.cs b/AdvancedC#Grammer/MySolution/MyConsoleApp/Work/AsyncAwaitWork.cs new file mode 100644 index 0000000..d2f3f32 --- /dev/null +++ b/AdvancedC#Grammer/MySolution/MyConsoleApp/Work/AsyncAwaitWork.cs @@ -0,0 +1,47 @@ +namespace MyConsoleApp.Work +{ + public class AsyncAwaitWork + { + private static Task Test() + { + System.Console.WriteLine("Start Test"); + Task t = Task.Delay(3000); + return t; + } + + private static async void TestAsync() + { + System.Console.WriteLine("Start Test"); + Task t = Task.Delay(3000); + await t; // 함수 내부에서는 동기, 함수 호출부에서는 비동기로 처리됨 + System.Console.WriteLine("End TestAsync"); + } + + private static async Task TestAsync2() + { + System.Console.WriteLine("Start Test"); + Task t = Task.Delay(3000); + await t; + System.Console.WriteLine("End TestAsync"); + + return 1; + } + + public async static void DoTest() + { + // Task t = Test(); + // t.Wait(); + + //TestAsync(); + await TestAsync2(); // 메인 함수도 동기로 처리됨 + + int res = await TestAsync2(); + + System.Console.WriteLine("While start"); + while (true) + { + + } + } + } +} \ No newline at end of file diff --git a/AdvancedC#Grammer/MySolution/MyConsoleApp/Work/LinqWork.cs b/AdvancedC#Grammer/MySolution/MyConsoleApp/Work/LinqWork.cs new file mode 100644 index 0000000..4f00b66 --- /dev/null +++ b/AdvancedC#Grammer/MySolution/MyConsoleApp/Work/LinqWork.cs @@ -0,0 +1,160 @@ +using System.Diagnostics; +using System.Security.Cryptography; + +namespace MyConsoleApp.Work +{ + public class LinqWork + { + private static List _players = new List(); + + private static void InitInstance() + { + Random rand = new Random(); + for (int i = 0; i < 100; i++) + { + ClassType type = ClassType.Knight; + switch (rand.Next(0, 3)) + { + case 1: + type = ClassType.Archer; + break; + case 2: + type = ClassType.Mage; + break; + } + + Player player = new Player() + { + ClassType = type, + Level = rand.Next(1, 100), + Hp = rand.Next(100, 1000), + Attack = rand.Next(5, 50) + }; + + for (int j = 0; j < 5; j++) + { + player.Items.Add(rand.Next(1, 100)); + } + + _players.Add(player); + } + } + + public static void DoTest() + { + InitInstance(); + + // Q1) 레벨 50 이상인 Knight를 레벨 오름차순으로 정렬 + + Stopwatch sw = new Stopwatch(); + sw.Start(); + // Without Linq + { + List players = GetHighLevelKnight(); + foreach (Player player in players) + { + System.Console.WriteLine($"[{player.Level}] {player.Hp}"); + } + } + System.Console.WriteLine($"{sw.ElapsedMilliseconds}ms"); + + System.Console.WriteLine("---------------------"); + + sw.Restart(); + // With Linq + { + var players = + from p in _players + where p.ClassType == ClassType.Knight && p.Level >= 50 + orderby p.Level + select p; + + foreach (Player player in players) + { + System.Console.WriteLine($"[{player.Level}] {player.Hp}"); + } + } + System.Console.WriteLine($"{sw.ElapsedMilliseconds}ms"); + + // from 중첩 + // Q2) id가 30 미만인 모든 아이템 목록 추출 + { + var playerItems = + from p in _players + from i in p.Items + where i < 30 + select new { p, i }; + + var li = playerItems.ToList(); + System.Console.WriteLine($"Count: {li.Count}"); + } + + // grouping + { + var playerByLevel = + from p in _players + group p by p.Level into g + orderby g.Key + select new { g.Key, Players = g }; + } + + // join + { + List levels = new List() { 1, 5, 10 }; + var players = + from p in _players + join l in levels + on p.Level equals l + select p; + } + + // Linq 표준 연산자 + { + var players = + from p in _players + where p.ClassType == ClassType.Knight && p.Level >= 50 + orderby p.Level + select p; + + var players2 = _players + .Where(p => p.ClassType == ClassType.Knight && p.Level >= 50) + .OrderBy(p => p.Level) + .Select(p => p); + } + } + + private static List GetHighLevelKnight() + { + List players = new List(); + foreach (Player player in _players) + { + if (player.ClassType != ClassType.Knight) + continue; + if (player.Level < 50) + continue; + + players.Add(player); + } + + players.Sort((lhs, rhs) => { return lhs.Level - rhs.Level; }); + + return players; + } + } + + public enum ClassType + { + Knight, + Archer, + Mage, + } + + public class Player + { + public ClassType ClassType { get; set; } + public int Level { get; set; } + public int Hp { get; set; } + public int Attack { get; set; } + public List Items { get; set; } = new List(); + } +} \ No newline at end of file diff --git a/AdvancedC#Grammer/MySolution/MySolution.sln b/AdvancedC#Grammer/MySolution/MySolution.sln new file mode 100644 index 0000000..bde7c21 --- /dev/null +++ b/AdvancedC#Grammer/MySolution/MySolution.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyConsoleApp", "MyConsoleApp\MyConsoleApp.csproj", "{4904D5DC-9923-40EE-9550-E99B74754381}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4904D5DC-9923-40EE-9550-E99B74754381}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4904D5DC-9923-40EE-9550-E99B74754381}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4904D5DC-9923-40EE-9550-E99B74754381}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4904D5DC-9923-40EE-9550-E99B74754381}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/HTML, CSS/test.html b/HTML, CSS/test.html index cd36782..08cee1d 100644 --- a/HTML, CSS/test.html +++ b/HTML, CSS/test.html @@ -19,6 +19,25 @@ td { border: 1px solid salmon; } + #para1 { + color: red; + font-weight: bold; + text-align: center; + } + .center { + text-align: center; + } + div { + background-color: lightblue; + width: 300px; + border: 15px solid green; + padding: 50px; + margin: 20px; + } + h1 { + border-bottom: 1px solid gray; + padding-bottom: 15px; + } @@ -30,12 +49,15 @@ enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
- Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore - eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, - sunt in culpa qui officia deserunt mollit anim id est laborum. + aute irure dolor + in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla + pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui + officia deserunt mollit anim id est laborum.

This is paragraph.

+

This is paragraph.

This is paragraph.

+

This is paragraph.

This is paragraph.

+
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod + tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim + veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea + commodo consequat. Duis aute irure dolor in reprehenderit in voluptate + velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat + cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id + est laborum. +
+