A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 10.1 :Basic LINQ queries

  LINQ is a powerful feature in C# that allows you to query data from various sources using a SQL-like syntax.

Step 1: Understand LINQ Basics

Before diving into LINQ queries, it's essential to understand the basic concepts of LINQ:

  • IEnumerable<T>: LINQ works with collections that implement the IEnumerable<T> interface. Most of the standard collections in C# like arrays, lists, and dictionaries implement this interface.

  • Lambda Expressions: LINQ uses lambda expressions to define anonymous functions. Lambda expressions are concise ways to represent methods without explicitly defining a separate method.

  • Query Expressions: LINQ queries are written using a SQL-like syntax, referred to as query expressions.

Step 2: Set Up Your Project

Create a new C# Console Application in Visual Studio or your preferred IDE.

Step 3: Import Necessary Libraries

Make sure to include the following using directive at the top of your C# file to use LINQ:

using System.Linq;

Step 4: Create a Sample Data Source

For the purpose of this tutorial, let's create a simple class and a list of objects. Consider the following class:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Now, create a list of Person objects in the Main method:

List<Person> people = new List<Person>
{
    new Person { Name = "Alice", Age = 25 },
    new Person { Name = "Bob", Age = 30 },
    new Person { Name = "Charlie", Age = 22 },
    new Person { Name = "David", Age = 35 }
};

Step 5: Basic LINQ Queries

Now, let's explore some basic LINQ queries:

Query 1: Filtering

// Get people who are older than 25
var result = from person in people
             where person.Age > 25
             select person;

foreach (var person in result)
{
    Console.WriteLine($"{person.Name} is {person.Age} years old.");
}

Query 2: Projection

// Select only the names of people
var names = from person in people
            select person.Name;

foreach (var name in names)
{
    Console.WriteLine($"Name: {name}");
}

Query 3: Ordering

// Order people by age in ascending order
var orderedPeople = from person in people
                    orderby person.Age
                    select person;

foreach (var person in orderedPeople)
{
    Console.WriteLine($"{person.Name} is {person.Age} years old.");
}

Query 4: Aggregation

// Calculate the average age of people
var averageAge = people.Average(person => person.Age);
Console.WriteLine($"Average Age: {averageAge}");

Step 6: Run and Experiment

Run your program and see the results of each query. Feel free to modify the queries and experiment with different LINQ operators.

This is a basic introduction to LINQ in C#, and there's much more you can do with LINQ, including joining, grouping, and more advanced operations. Explore the official Microsoft documentation for LINQ for a comprehensive understanding: LINQ (Language-Integrated Query).