From 80747f704e5150e3371f30dd3d94a13f52c42e93 Mon Sep 17 00:00:00 2001 From: moravcikj24 Date: Tue, 19 Nov 2024 08:40:44 +0100 Subject: [PATCH] Linq - knihy final --- LINQ - kniha/Program.cs | 39 ++++++++++++++++--- LINQ/Program.cs | 16 ++++++-- .../AppsLab-033-NuGet.csproj | 4 ++ src/AppsLab-033-NuGet/Program.cs | 19 ++++++++- 4 files changed, 67 insertions(+), 11 deletions(-) diff --git a/LINQ - kniha/Program.cs b/LINQ - kniha/Program.cs index 861fc317..56a7d6f7 100644 --- a/LINQ - kniha/Program.cs +++ b/LINQ - kniha/Program.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System.Collections; +using System.Collections.Generic; +using System.Runtime.ExceptionServices; namespace LINQ___kniha { @@ -12,17 +14,42 @@ public static void Main(string[] args) new Kniha { Autor = "Anna Hruskova",RokVydania = 2009, JePreDeti = false }, new Kniha { Autor = "Bob", RokVydania = 2022, JePreDeti = true }, new Kniha { Autor = "Charlie", RokVydania = 2018, JePreDeti = true }, - new Kniha { Autor = "Bob", RokVydania = 2020 , JePreDeti = false}, + new Kniha { Autor = "Imrich", RokVydania = 2020 , JePreDeti = false}, + new Kniha { Autor = "Imrich 2", RokVydania = 2020 , JePreDeti = false}, + new Kniha { Autor = "Imrich 3", RokVydania = 2020 , JePreDeti = false}, }; List knihaJePreDeti = Knihy.Where(kniha => kniha.JePreDeti == true).ToList(); - foreach (Kniha kniha in knihaJePreDeti) + List knihyOdNajstarsej = Knihy.OrderBy(kniha => kniha.RokVydania).ToList(); + List kinhyOdNajmladsej = Knihy.OrderByDescending(kniha => kniha.RokVydania).ToList(); + List knihyPreDetiOdNajstarsej = Knihy.Where(kniha => kniha.JePreDeti).OrderBy(kniha => kniha.RokVydania).ToList(); //!kniha = negacia cize False = True a naopak + + var knihyPodlaRoku = Knihy.GroupBy(u => u.RokVydania).Select(grp => grp.ToList()).ToList(); + + Kniha Prvaknizaold = Knihy[0]; + Kniha prvakniha = Knihy.Last(); + + foreach (List skupinka in knihyPodlaRoku) + { + Console.WriteLine($"Skupinka:"); + + foreach (Kniha kniha in skupinka) + { + Console.WriteLine($" Autor: {kniha.Autor},{kniha.RokVydania}, {kniha.JePreDeti} je pre deti "); + } + + } + Console.WriteLine(); + var knihyPodlaRoku_IDictionary = Knihy.GroupBy(o => o.RokVydania).ToDictionary(g => g.Key, g => g.ToList()); + foreach (var skupinka in knihyPodlaRoku_IDictionary) { - Console.WriteLine($" Autor: {kniha.Autor}, {kniha.RokVydania}. Je pre deti ? { kniha.JePreDeti}"); + Console.WriteLine($"Skupinka {skupinka.Key}"); + foreach (Kniha kniha in skupinka.Value) + { + Console.WriteLine($" Autor: {kniha.Autor},{kniha.RokVydania}, {kniha.JePreDeti} je pre deti "); + } } } - - } } \ No newline at end of file diff --git a/LINQ/Program.cs b/LINQ/Program.cs index 19144d66..8b1cd48b 100644 --- a/LINQ/Program.cs +++ b/LINQ/Program.cs @@ -12,19 +12,29 @@ static void Main(string[] args) new Student { Name = "Charlie", Age = 18 } }; - /* List tinedzeriStudenti_stary = new List(); + + //Prehladavanie list, ziskali sme vsetkych studentov s menom Anna + List tinedzeriStudenti_stary = new List(); foreach(Student student in students) { - if (student.Age < 20) + if (student.Name.Contains("Anna")) { tinedzeriStudenti_stary.Add(student); } } - + /* List tinedzeristudenti = students.Where(student => student.Age < 20).ToList(); foreach (Student student in tinedzeriStudenti_stary) { Console.WriteLine($"Stary sposob: Student {student.Name} ma {student.Age} a je tinedzer"); + } + List tinedzeriStudenti_stary = new List(); + foreach(Student student in students) + { + if (student.Age < 20) + { + tinedzeriStudenti_stary.Add(student); + } }*/ diff --git a/src/AppsLab-033-NuGet/AppsLab-033-NuGet.csproj b/src/AppsLab-033-NuGet/AppsLab-033-NuGet.csproj index 14b7e5b8..df8821f8 100644 --- a/src/AppsLab-033-NuGet/AppsLab-033-NuGet.csproj +++ b/src/AppsLab-033-NuGet/AppsLab-033-NuGet.csproj @@ -8,4 +8,8 @@ enable + + + + diff --git a/src/AppsLab-033-NuGet/Program.cs b/src/AppsLab-033-NuGet/Program.cs index 3751555c..0aa83237 100644 --- a/src/AppsLab-033-NuGet/Program.cs +++ b/src/AppsLab-033-NuGet/Program.cs @@ -1,2 +1,17 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using Newtonsoft.Json; +using System; + +namespace AppsLab_033_NuGet +{ + class Program + { + static void Main(string[] args) + { + var person = new { Name = "Jano", Age = 25 }; + + string json = JsonConvert.SerializeObject(person, Formatting.Indented); + + Console.WriteLine(json); + } + } +} \ No newline at end of file