Various Linq Methods In C#

LINQ (Language Integrated Query) is a powerful feature in C# that allows developers to query data in a variety of data sources, including arrays, lists, and databases. Here are some common LINQ methods and examples of how they can be used.

Where()

This method filters a sequence of values based on a given predicate. For example, to filter a list of integers and return only the even numbers.

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
var evenNumbers = numbers.Where(n => n % 2 == 0);

Select()

This method projects each element of a sequence into a new form. For example, to select a list of strings containing the names of each object in a list of person objects.

List<Person> people = new List<Person> { new Person { Name = "John", Age = 25 }, new Person { Name = "Jane", Age = 30 } };
var names = people.Select(p => p.Name);

OrderBy()

This method sorts the elements of a sequence in ascending order according to a key. For example, to sort a list of strings alphabetically

List<string> words = new List<string> { "apple", "banana", "cherry" };
var sortedWords = words.OrderBy(w => w);

GroupBy()

This method groups the elements of a sequence according to a specified key. For example, to group a list of products by category.

List<Product> products = new List<Product> { new Product { Name = "Computer", Category = "Electronics" }, new Product { Name = "Book", Category = "Literature" } };
var groupedProducts = products.GroupBy(p => p.Category);

Aggregate()

This method applies a specified function to the elements of a sequence and returns a single value. For example, to compute the sum of a list of integers.

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var sum = numbers.Aggregate((a, b) => a + b);

Join()

Correlates the elements of two sequences based on matching keys. For example, the following code joins two lists of students based on their class ID.

List<Student> students = // some list of students
List<Class> classes = // some list of classes
var query = from student in students
            join cls in classes on student.ClassId equals cls.Id
            select new { student.Name, cls.Name };

Any()

Determines whether any element of a sequence satisfies a condition. For example, the following code checks if there are any even numbers in a list of integers

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
bool anyEven = numbers.Any(x => x % 2 == 0);

Count()

Returns the number of elements in a sequence. For example, the following code counts the number of elements in a list of integers

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int count = numbers.Count();

First(), FirstOrDefault(), Last(), LastOrDefault()

Returns the first or last element of a collection that satisfies a given predicate. For example, the following code will return the first even number in a list of integers.

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = numbers.First(x => x % 2 == 0);

These are just a few examples of the many LINQ methods available in C#. The actual method you would use depends on the specific requirements of your application.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories