What is LINQ and how is it used in C#

LINQ (Language Integrated Query) is a powerful feature in C# that enables developers to write queries directly in the language syntax, making data manipulation easier and more readable. LINQ can be used to query various data sources, such as collections, databases, XML documents, and more.

With LINQ, you can perform operations such as filtering, sorting, grouping, and aggregating data without the need for complex SQL statements or additional libraries. This greatly enhances productivity and reduces the potential for errors in data handling.

Example of LINQ in C#

// Sample list of integers List numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Using LINQ to find even numbers var evenNumbers = from n in numbers where n % 2 == 0 select n; // Output the results foreach (var num in evenNumbers) { Console.WriteLine(num); }

LINQ C# Language Integrated Query data manipulation collections databases XML queries filtering sorting grouping