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.2 :LINQ to Objects

 LINQ is a powerful feature in C# that allows you to query and manipulate data in a declarative and SQL-like syntax. LINQ to Objects specifically enables you to query in-memory collections like arrays, lists, and other IEnumerable<T> types.

Let's create a simple console application to demonstrate LINQ to Objects with step-by-step explanations.

Step 1: Create a new Console Application

Open Visual Studio and create a new Console Application. Name it as you wish.

Step 2: Define a Sample Data Collection

For this tutorial, let's create a simple class and a collection of objects:

class Person

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

class Program
{
    static void Main()
    {
        List<Person> people = new List<Person>
        {
            new Person { Name = "John", Age = 25 },
            new Person { Name = "Alice", Age = 30 },
            new Person { Name = "Bob", Age = 22 },
            new Person { Name = "Eva", Age = 28 },
            new Person { Name = "Charlie", Age = 35 }
        };

        // LINQ queries will be applied to this collection
    }
}

Step 3: Basic LINQ Queries

Now, let's start with some basic LINQ queries.

Filtering

var adults = from person in people
             where person.Age >= 18
             select person;

foreach (var adult in adults)
{
    Console.WriteLine($"{adult.Name} is an adult.");
}

Projection

var namesOnly = from person in people
                select person.Name;

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

Step 4: Ordering and Aggregation

Ordering

var orderedByAge = from person in people
                   orderby person.Age
                   select person;

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

Aggregation

int averageAge = people.Average(person => person.Age);
Console.WriteLine($"Average Age: {averageAge}");

Step 5: Advanced LINQ Queries

Grouping

var groupedByAge = from person in people
                   group person by person.Age;

foreach (var group in groupedByAge)
{
    Console.WriteLine($"People aged {group.Key}:");
    foreach (var person in group)
    {
        Console.WriteLine($"  {person.Name}");
    }

}

Joining

var departments = new List<string> { "HR", "IT", "Finance", "Marketing" };

var peopleWithDepartments = from person in people
                            join department in departments
                            on person.Age % departments.Count
                            equals departments.IndexOf(department)
                            select new { person.Name, Department = department };

foreach (var person in peopleWithDepartments)
{
    Console.WriteLine($"{person.Name} works in {person.Department} department.");
}

Conclusion

This tutorial covers the basics of LINQ to Objects in C#. You can explore more advanced features and operations, such as Skip, Take, Any, All, etc., to further enhance your querying capabilities. LINQ is a versatile tool that can greatly simplify data manipulation in your C# applications.