// Find all even numbers using LINQ int[] numbers = { 2, 5, 8, 10, 13 }; var evenNumbers = from num in numbers where num % 2 == 0 select num; Console.WriteLine(string.Join(", ", evenNumbers)); // Output: 2, 8, 10
// Group products by category using LINQ ListIn conclusion, System.Linq is a powerful package library that provides developers with a wide range of query operators to manipulate data from different sources. By using LINQ, developers can easily and efficiently filter, sort, group, and aggregate data without writing complex loops and nested conditions.products = new List { new Product { Name = "Milk", Category = "Dairy", Price = 2.5 }, new Product { Name = "Cheese", Category = "Dairy", Price = 3.25 }, new Product { Name = "Bread", Category = "Bakery", Price = 1.5 }, new Product { Name = "Cake", Category = "Bakery", Price = 10.0 } }; var groupedProducts = from prod in products group prod by prod.Category into prodGroup select new { Category = prodGroup.Key, Products = prodGroup.ToList() }; foreach (var group in groupedProducts) { Console.WriteLine(group.Category); foreach (var product in group.Products) { Console.WriteLine($"- {product.Name} (${product.Price})"); } } // Output: // Dairy // - Milk ($2.50) // - Cheese ($3.25) // Bakery // - Bread ($1.50) // - Cake ($10.00)