From c409983da094b2f4e9a39002e8d01e30a9cff458 Mon Sep 17 00:00:00 2001 From: divyanshsahu1606 Date: Wed, 9 Aug 2023 13:13:27 +0530 Subject: [PATCH 1/4] day2 --- .../exercise-3/AnimalExercise/Program.cs | 66 ++++++++++- .../VehicleManagementSystem/Program.cs | 111 +++++++++++++++++- 2 files changed, 170 insertions(+), 7 deletions(-) diff --git a/week-1/day-2/exercise-3/AnimalExercise/Program.cs b/week-1/day-2/exercise-3/AnimalExercise/Program.cs index 5928b7cb..3b8cc7f1 100644 --- a/week-1/day-2/exercise-3/AnimalExercise/Program.cs +++ b/week-1/day-2/exercise-3/AnimalExercise/Program.cs @@ -1,10 +1,70 @@ namespace AnimalExercise { - internal class Program + internal abstract class Animal { + String Name; + int Age; + + protected abstract void MakeSound(); + static void Main(string[] args) { - // Create a list of Animal objects, add Dog and Cat instances, and call their methods + Console.WriteLine("animals"); + List animals = new List(); + + Dog dog = new Dog { Name = "abc", Age = 4 }; + Dog dog2 = new Dog { Name = "kkk", Age = 5 }; + Cat cat = new Cat { Name = "ppp", Age = 6 }; + Cat cat2 = new Cat { Name = "ppp", Age = 6 }; + + animals.Add(dog); + animals.Add(dog2); + animals.Add(cat); + animals.Add(cat2); + + foreach (var animal in animals) + { + Console.WriteLine("Name:" + animal.Name + " Age:" + animal.Age); + animal.MakeSound(); + + if (animal is Imovable movable) + { + movable.move(); + + } + } + + } } -} \ No newline at end of file + + class Dog : Animal, Imovable + { + protected override void MakeSound() + { + Console.WriteLine("woof"); + } + public void move() + { + Console.WriteLine("dog is moving"); + } + + } + class Cat : Animal, Imovable + { + protected override void MakeSound() + { + Console.WriteLine("Meow"); + } + public void move() + { + Console.WriteLine("cat is moving"); + } + + } + interface Imovable + { + public void move(); + } +} + diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs index 77395b3a..d4c8ff36 100644 --- a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs @@ -1,10 +1,113 @@ -namespace VehicleManagementSystem +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DAY2 { - internal class Program + internal class Vehicle { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + { + Car car = new Car(); + Truck truck = new Truck(); + + car.Drive(); + truck.Drive(); + } + { + VehicleLogger logger = VehicleLogger.Instance; + + logger.Log("Car ignition turned on."); + logger.Log("Truck started."); + + } + { + VehicleFactory carFactory = new CarFactory(); + VehicleFactory truckFactory = new TruckFactory(); + + carFactory.DoSomethingWithVehicle(); + truckFactory.DoSomethingWithVehicle(); + + } + + } + + + } -} \ No newline at end of file +} +public interface Ivehicle +{ + void Drive(); + + +} +public class Car : Ivehicle +{ + public void Drive() + { + Console.WriteLine("Car is moving."); + } +} +public class Truck : Ivehicle +{ + public void Drive() + { + Console.WriteLine("Truck is moving."); + } +} +public class VehicleLogger +{ + private static VehicleLogger instance; + private VehicleLogger() + { + + } + public static VehicleLogger Instance + { + get + { + if (instance == null) + { + instance = new VehicleLogger(); + } + return instance; + } + } + public void Log(string message) + { + Console.WriteLine($"Logging: {message}"); + } +} +public abstract class VehicleFactory +{ + public abstract Ivehicle CreateVehicle(); + + public void DoSomethingWithVehicle() + { + Ivehicle vehicle = CreateVehicle(); + Console.WriteLine("Do something with vehicle "); + vehicle.Drive(); + } + + +} +public class CarFactory : VehicleFactory +{ + public override Ivehicle CreateVehicle() + { + return new Car(); + } +} + +public class TruckFactory : VehicleFactory +{ + public override Ivehicle CreateVehicle() + { + return new Truck(); + } +} From d50c158bfa8fa7c003168ce8e42acd0b2f99bab1 Mon Sep 17 00:00:00 2001 From: divyanshsahu1606 Date: Wed, 9 Aug 2023 13:18:06 +0530 Subject: [PATCH 2/4] day2 --- week-1/day-2/exercise-1/Circle/Circle.cs | 24 +++++++++++------ week-1/day-2/exercise-1/Circle/Program.cs | 12 ++++++--- .../day-3/exercise-1/FactorialApp/Program.cs | 27 +++++++++++-------- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/week-1/day-2/exercise-1/Circle/Circle.cs b/week-1/day-2/exercise-1/Circle/Circle.cs index 065ffa5d..ef0bbf0a 100644 --- a/week-1/day-2/exercise-1/Circle/Circle.cs +++ b/week-1/day-2/exercise-1/Circle/Circle.cs @@ -1,13 +1,21 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; + -namespace Circle +public class Circle { - internal class Circle + public double Radius { get; set; } + + public Circle(double radius) + { + Radius = radius; + } + + public double GetArea() { - // Implement the Circle class here + return Math.PI * Math.Pow(Radius, 2); } + public double GetCircumference() + { + return 2 * Math.PI * Radius; + } + } diff --git a/week-1/day-2/exercise-1/Circle/Program.cs b/week-1/day-2/exercise-1/Circle/Program.cs index 0df91b0d..3560b0ff 100644 --- a/week-1/day-2/exercise-1/Circle/Program.cs +++ b/week-1/day-2/exercise-1/Circle/Program.cs @@ -1,10 +1,16 @@ -namespace Circle +namespace DAY2 { internal class Program { static void Main(string[] args) { - // Create a Circle object and display its area and circumference + double radius = 10; + Circle circle = new Circle(radius); + double area = circle.GetArea(); + double circumference = circle.GetCircumference(); + Console.WriteLine($"Area:{area}"); + Console.WriteLine($"circumference:{circumference}"); + Console.WriteLine("Hello, World!"); } } -} \ No newline at end of file +} diff --git a/week-1/day-3/exercise-1/FactorialApp/Program.cs b/week-1/day-3/exercise-1/FactorialApp/Program.cs index f116eeca..43fdd8c5 100644 --- a/week-1/day-3/exercise-1/FactorialApp/Program.cs +++ b/week-1/day-3/exercise-1/FactorialApp/Program.cs @@ -1,20 +1,25 @@ -namespace FactorialApp +namespace program.cs { - public class Program + internal class Program { - public static void Main() + static void Main(string[] args) { - Console.Write("Enter a number: "); - int number = int.Parse(Console.ReadLine()); + Console.WriteLine(" Factorial"); + Console.WriteLine(" Enter a number here"); + Int32 num = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Number is " + num); - long factorial = CalculateFactorial(number); + Int32 result = num; + Int32 count = 1; - Console.WriteLine($"The factorial of {number} is: {factorial}"); - } + while (count < num) + { + result = result * count; + Console.WriteLine(result); + count++; + } + Console.WriteLine("Factorial is " + result); - public static long CalculateFactorial(int number) - { - throw new NotImplementedException(); } } } \ No newline at end of file From 9083904e55cee93dde0ed119b24cc970c640bd66 Mon Sep 17 00:00:00 2001 From: divyanshsahu1606 Date: Wed, 9 Aug 2023 15:22:42 +0530 Subject: [PATCH 3/4] Day2-ex-2 --- week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs b/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs index a4ec62d9..8c02d7d6 100644 --- a/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs +++ b/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs @@ -223,7 +223,7 @@ public int[] DijkstraShortestPath(int source) visited[i] = false; } - distances[source] = int.MinValue; + distances[source] = 0; for (int count = 0; count < vertices - 1; count++) { From 4ae1944065af0ea941a286dd8bb5ff0f5571ae53 Mon Sep 17 00:00:00 2001 From: divyanshsahu1606 Date: Thu, 10 Aug 2023 17:05:24 +0530 Subject: [PATCH 4/4] Day-4 --- .../day-4/exercise-1/StackApp/ICustomStack.cs | 37 +++++++++-- week-1/day-4/exercise-1/StackApp/Program.cs | 34 +++++++--- .../day-4/exercise-2/IteratorsApp/Program.cs | 63 ++++++++++++++++--- .../LinqToObjectsApp/LinqToObjectsApp.sln | 2 +- .../exercise-3/LinqToObjectsApp/Program.cs | 43 ++++++++++++- .../day-4/exercise-4/LinqToXmlApp/Program.cs | 27 +++++++- .../QueryOptimizationApp/Program.cs | 8 ++- .../exercise-6/LinqFilterAndSort/Program.cs | 32 +++++++++- .../exercise-7/LinqListNumbers/Program.cs | 31 ++++++++- .../exercise-8/LinqGroupAggregate/Program.cs | 43 +++++++++++++ 10 files changed, 289 insertions(+), 31 deletions(-) diff --git a/week-1/day-4/exercise-1/StackApp/ICustomStack.cs b/week-1/day-4/exercise-1/StackApp/ICustomStack.cs index 55f3936d..b97dc75d 100644 --- a/week-1/day-4/exercise-1/StackApp/ICustomStack.cs +++ b/week-1/day-4/exercise-1/StackApp/ICustomStack.cs @@ -1,9 +1,38 @@ namespace StackApp { - internal interface ICustomStack + class Stack { - void Push(T item); - T Pop(); - bool IsEmpty(); + private List list = new List(); + + public void Push(T item) + { + list.Add(item); + } + + public T Pop() + { + if (list.Count == 0) + throw new Exception("Stack is Empty"); + T item = list[list.Count - 1]; + list.RemoveAt(list.Count - 1); + return item; + } + public bool IsEmpty() + { + return list.Count == 0; + } } + + } +class Person +{ + public string Name { get; set; } + public int Age { get; set; } + + public Person(string name, int age) + { + Name = name; + Age = age; + } +} \ No newline at end of file diff --git a/week-1/day-4/exercise-1/StackApp/Program.cs b/week-1/day-4/exercise-1/StackApp/Program.cs index e089f665..086f6b49 100644 --- a/week-1/day-4/exercise-1/StackApp/Program.cs +++ b/week-1/day-4/exercise-1/StackApp/Program.cs @@ -1,16 +1,34 @@ -namespace StackApp +using System; + +namespace StackApp { internal class Program { static void Main(string[] args) { - //ICustomStack intStack = new CustomStack(); - //intStack.Push(1); - //intStack.Push(2); - //intStack.Push(3); - //Console.WriteLine(intStack.Pop()); // Output: 3 - //Console.WriteLine(intStack.Pop()); // Output: 2 - //Console.WriteLine(intStack.IsEmpty()); // Output: False + Stack intstack = new Stack(); + intstack.Push(1); + intstack.Push(2); + intstack.Push(3); + intstack.Push(4); + intstack.Push(5); + Console.WriteLine(intstack.Pop()); //out=5 + + Stack stringstack = new Stack(); + stringstack.Push("Namaste"); + stringstack.Push("Kem"); + stringstack.Push("Cho"); + stringstack.Push("Maja"); + stringstack.Push("ma"); + Console.WriteLine(stringstack.Pop()); //out=ma + + Stack PersonStack = new Stack(); + PersonStack.Push(new Person("Divyansh", 22)); + PersonStack.Push(new Person("Arpit", 23)); + PersonStack.Push(new Person("Mradul", 22)); + PersonStack.Push(new Person("Devyan", 23)); + PersonStack.Push(new Person("Pawan", 23)); + Console.WriteLine(PersonStack.Pop().Name); //out=pawan } } } \ No newline at end of file diff --git a/week-1/day-4/exercise-2/IteratorsApp/Program.cs b/week-1/day-4/exercise-2/IteratorsApp/Program.cs index f3cb1ae8..5bd32109 100644 --- a/week-1/day-4/exercise-2/IteratorsApp/Program.cs +++ b/week-1/day-4/exercise-2/IteratorsApp/Program.cs @@ -1,20 +1,69 @@ -namespace IteratorsApp +using System; +using System.Collections; + +namespace IteratorsApp { internal class Program { static void Main(string[] args) { - var fibonacci = FibonacciSequence().Take(10); - foreach (int number in fibonacci) + FibonacciIterator fibonacciIter = new FibonacciIterator(); + IEnumerator enumerator = fibonacciIter.GetEnumerator(); + + for (int i = 0; i < 10; i++) { - Console.WriteLine(number); + enumerator.MoveNext(); + Console.WriteLine(enumerator.Current); } } // https://www.c-sharpcorner.com/UploadFile/5ef30d/understanding-yield-return-in-C-Sharp/ // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/yield - public static IEnumerable FibonacciSequence() + + } +} +public class FibonacciIterator : IEnumerable +{ + public IEnumerator GetEnumerator() + { + return new FibonacciEnumerator(); + } +} + +public class FibonacciEnumerator : IEnumerator +{ + private int prev = 0; + private int curr = 1; + private int count = 0; + + public object Current => curr; + + public bool MoveNext() + { + if (count == 0) { - throw new NotImplementedException(); + count++; + return true; } + else if (count == 1) + { + count++; + return true; + } + else + { + count++; + int nextValue = prev + curr; + prev = curr; + curr = nextValue; + return true; + } + } + + public void Reset() + { + prev = 0; + curr = 1; + count = 0; } -} \ No newline at end of file +} + diff --git a/week-1/day-4/exercise-3/LinqToObjectsApp/LinqToObjectsApp.sln b/week-1/day-4/exercise-3/LinqToObjectsApp/LinqToObjectsApp.sln index b620a2b6..4604d313 100644 --- a/week-1/day-4/exercise-3/LinqToObjectsApp/LinqToObjectsApp.sln +++ b/week-1/day-4/exercise-3/LinqToObjectsApp/LinqToObjectsApp.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.5.33516.290 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqToObjectsApp", "LinqToObjectsApp.csproj", "{0ACCC0CF-62BA-4610-AFD8-4AFD6B2DD1F9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqToObjectsApp", "LinqToObjectsApp.csproj", "{0ACCC0CF-62BA-4610-AFD8-4AFD6B2DD1F9}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs b/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs index 95e8ac84..3de8769f 100644 --- a/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs +++ b/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs @@ -1,4 +1,7 @@ -namespace LinqToObjectsApp +using System.Collections.Generic; + + +namespace LinqToObjectsApp { internal class Program { @@ -18,14 +21,50 @@ static void Main(string[] args) //2. Get all people above 30 //3. Sort people by name //4. Project/Select only Name and Country of all people + var peopleFromUSA = people.Where(person => person.Country == "USA").ToList(); + + // usa ppl + Console.WriteLine("People from USA:"); + foreach (var person in peopleFromUSA) + { + Console.WriteLine($"{person.Name}, {person.Age}, {person.Country}"); + } + + // 30+ + var peopleAbove30 = people.Where(person => person.Age > 30).ToList(); + + Console.WriteLine("\nPeople above 30:"); + foreach (var person in peopleAbove30) + { + Console.WriteLine($"{person.Name}, {person.Age}, {person.Country}"); + } + // name sort + var sortedPeople = people.OrderBy(person => person.Name).ToList(); + + Console.WriteLine("\nPeople sorted by name:"); + foreach (var person in sortedPeople) + { + Console.WriteLine($"{person.Name}, {person.Age}, {person.Country}"); + } + + // name,country + var projectedPeople = people.Select(person => new { person.Name, person.Country }).ToList(); + + Console.WriteLine("\nProjected list (Name and Country):"); + foreach (var person in projectedPeople) + { + Console.WriteLine($"{person.Name}, {person.Country}"); + } } } public class Person { public string Name { get; set; } + public int Age { get; set; } public string Country { get; set; } + } -} \ No newline at end of file +} diff --git a/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs b/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs index 315df143..94b7c0e5 100644 --- a/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs +++ b/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs @@ -1,4 +1,6 @@ -namespace LinqToXmlApp +using System.Xml.Linq; + +namespace LinqToXmlApp { internal class Program { @@ -32,12 +34,33 @@ static void Main(string[] args) "; + // Create an XDocument object from the XML string + XDocument xDocument = XDocument.Parse(xmlString); // Write the title of all books to the console + var allTitles = from book in xDocument.Descendants("Book") + select book.Element("Title").Value; + + Console.WriteLine("All Book Titles:"); + foreach (var title in allTitles) + { + Console.WriteLine(title); + } + + Console.WriteLine(); // Write the title of all books with genre "Genre 1" to the console + var genre1Titles = from book in xDocument.Descendants("Book") + where book.Element("Genre").Value == "Genre 1" + select book.Element("Title").Value; + + Console.WriteLine("Book Titles in Genre 1:"); + foreach (var title in genre1Titles) + { + Console.WriteLine(title); + } } } -} \ No newline at end of file +} diff --git a/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs b/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs index b18cea78..703e111f 100644 --- a/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs +++ b/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs @@ -9,13 +9,15 @@ static void Main(string[] args) List data = GenerateRandomNumbers(10000000); Stopwatch sw = Stopwatch.StartNew(); // Current implementation - var originalQuery = data.Where(x => x > 100).OrderByDescending(x => x).Take(10); + + sw.Start(); + var originalQuery = data.Where(x => x > 100).OrderByDescending(x => x).Take(10).ToList(); sw.Stop(); Console.WriteLine("Original Query: {0} ms", sw.ElapsedMilliseconds); // Optimized implementation sw.Restart(); - + var optimizedQuery = data.Where(x => x > 100).Take(10).OrderBy(x => -x).ToList(); sw.Stop(); Console.WriteLine("Optimized Query: {0} ms", sw.ElapsedMilliseconds); } @@ -34,4 +36,4 @@ static List GenerateRandomNumbers(int count) return numbers; } } -} \ No newline at end of file +} diff --git a/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs b/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs index 5c6d6240..19591aeb 100644 --- a/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs +++ b/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs @@ -1,12 +1,40 @@ -namespace LinqFilterAndSort +using System.Runtime.Intrinsics.X86; +using System; + +namespace LinqFilterAndSort { internal class Program { static void Main(string[] args) { // Create a list of Person objects + List people = new List + { + new Person { FirstName = "Sachin", LastName = "Tendulkar", Age = 50 }, + new Person { FirstName = "Virat", LastName = "Kohli", Age = 32 }, + new Person { FirstName = "MS", LastName = "Dhoni", Age = 40 }, + + }; + // Use LINQ to filter and sort the list + var filteredAndSorted = people + .Where(person => person.Age >= 18) + .OrderBy(person => person.LastName) + .ThenBy(person => person.FirstName) + .ToList(); + // Print the filtered and sorted list of people to the console + foreach (var person in filteredAndSorted) + { + Console.WriteLine($"Name: {person.FirstName} {person.LastName}, Age: {person.Age}"); + } + } } -} \ No newline at end of file +} +public class Person +{ + public string FirstName { get; set; } + public string LastName { get; set; } + public int Age { get; set; } +} diff --git a/week-1/day-4/exercise-7/LinqListNumbers/Program.cs b/week-1/day-4/exercise-7/LinqListNumbers/Program.cs index 6caadd07..41cb8a4e 100644 --- a/week-1/day-4/exercise-7/LinqListNumbers/Program.cs +++ b/week-1/day-4/exercise-7/LinqListNumbers/Program.cs @@ -1,4 +1,6 @@ -namespace LinqListNumbers +using System.Runtime.Intrinsics.X86; + +namespace LinqListNumbers { internal class Program { @@ -9,10 +11,35 @@ static void Main(string[] args) // Use LINQ to perform the following operations: // 1. Find all even numbers + var evenNumbers = numbers.Where(n => n % 2 == 0); + Console.WriteLine("Even Numbers:"); + foreach (var num in evenNumbers) + { + Console.WriteLine(num); + } + // 2. Find all numbers greater than a specific value (e.g., 20) + int specificValue = 20; + var greaterThanSpecific = numbers.Where(n => n > specificValue); + Console.WriteLine("Numbers Greater Than " + specificValue + ":"); + foreach (var num in greaterThanSpecific) + { + Console.WriteLine(num); + } // 3. Calculate the sum of all numbers + int sum = numbers.Sum(); + Console.WriteLine("Sum of Numbers: " + sum); + // 4. Calculate the average of all numbers + double average = numbers.Average(); + Console.WriteLine("Average of Numbers: " + average); + // 5. Find the minimum and maximum values in the list + int min = numbers.Min(); + int max = numbers.Max(); + Console.WriteLine("Minimum Value: " + min); + Console.WriteLine("Maximum Value: " + max); + } } -} \ No newline at end of file +} diff --git a/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs b/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs index 04dbd615..b9a26434 100644 --- a/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs +++ b/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs @@ -20,6 +20,49 @@ static void Main(string[] args) // 2. Count the number of products in each category // 3. Calculate the total price of products in each category // 4. Find the most expensive product in each category + var productsByCategory = products.GroupBy(p => p.Category); + foreach (var group in productsByCategory) + { + Console.WriteLine("Category: " + group.Key); + foreach (var product in group) + { + Console.WriteLine(" " + product.Name); + } + } + + // 2. Count the number of products in each category + var productCountsByCategory = productsByCategory.Select(group => new + { + Category = group.Key, + Count = group.Count() + }); + foreach (var count in productCountsByCategory) + { + Console.WriteLine("Category: " + count.Category + ", Count: " + count.Count); + } + + // 3. Calculate the total price of products in each category + var totalPriceByCategory = productsByCategory.Select(group => new + { + Category = group.Key, + TotalPrice = group.Sum(p => p.Price) + }); + foreach (var totalPrice in totalPriceByCategory) + { + Console.WriteLine("Category: " + totalPrice.Category + ", Total Price: " + totalPrice.TotalPrice); + } + + // 4. Find the most expensive product in each category + var mostExpensiveByCategory = productsByCategory.Select(group => new + { + Category = group.Key, + MostExpensiveProduct = group.OrderByDescending(p => p.Price).FirstOrDefault() + }); + foreach (var expensive in mostExpensiveByCategory) + { + Console.WriteLine("Category: " + expensive.Category + ", Most Expensive: " + expensive.MostExpensiveProduct?.Name); + } + } } } \ No newline at end of file