Linq 101

A beginner's guide to LINQ

Introduction

In the ever-evolving world of software development, we all aspire to craft elegant solutions that not only solve problems but do so with utmost efficiency. Microsoft's LINQ has transformed the way we work with data, serving as a valuable tool for developers and in this blog post, we'll explore how LINQ can help you write code that's cleaner, efficient, and more expressive.

What is LINQ?

LINQ, which stands for Language Integrated Query, is a powerful feature introduced by Microsoft in the .NET framework that allows developers to query and manipulate data using a unified language syntax. LINQ provides a more readable way to work with different data sources such as collections, databases, XML, and more.

With LINQ, developers can write queries to filter, sort, group, and join data without having to integrate or use external tools or libraries. LINQ is strongly typed, which means they are checked for correctness at compile-time, reducing the likelihood of runtime errors.

Why do we need LINQ?

In addition to enhancing code efficiency, LINQ offers several other advantages:

  • LINQ offers a unified approach to data querying and transformation across a wide range of data types and sources.

  • LINQ's tight integration with the .NET ecosystem greatly simplifies the debugging process.

  • LINQ's integration with C# and other .NET languages allows us to harness the full power of these languages, making advanced features like lambda expressions available for use in your queries.

  • LINQ simplifies code by offering a readable and expressive syntax, improving maintainability. Strong typing and reusability of queries reduce errors and enhance long-term code management.

To sum up, LINQ's advantages in code readability, strong typing, and query reusability make it a preferred choice in various development scenarios.

How to write LINQ queries?

💡 Understanding the LINQ syntax:

The syntax is straightforward if you are familiar with the C# programming language. Let's try to understand some basic LINQ syntaxes.

🔶 from clause: This clause specifies the data source and the range variable. This is similar to the traditional for loop.

var query = from item in collection
            select item;

🔶 where clause: This clause is used to filter data based on a condition.

var query = from item in collection
            where item.Property == someValue
            select item;

🔶 select clause: This clause defines the shape of the result set or specifies what data to project from the source.

var query = from item in collection
            select item.Property;

🔶 orderBy clause: This clause is used for sorting data in either ascending or descending order.

var query = from item in collection
            orderby item.Property ascending
            select item;

🔶 group clause: This clause is used to group data based on a key.

var query = from item in collection
            group item by item.Category into grouped
            select new
            {
                Category = grouped.Key,
                Items = grouped.ToList()
            };

🔶 join clause: This clause is used to join data from multiple sources.

var query = from item in collection1
            join otherItem in collection2 on item.Key equals otherItem.Key
            select new
            {
                Item = item,
                OtherItem = otherItem
            };

🔶 let clause: This clause allows you to create a temporary variable in the query.

var query = from item in collection
            let total = item.Quantity * item.Price
            select new
            {
                Item = item,
                Total = total
            };

🔶 into clause: This clause is used to create subqueries or groupings within the main query.

var query = from item in collection
            group item by item.Category into grouped
            where grouped.Count() > 3
            select grouped.Key;

These syntax examples are quite self-explanatory. Observe how they primarily adhere to a common structure. It becomes more straightforward with practice.

⌨️ Writing LINQ queries:

In this section, we'll explore the fundamental aspects of crafting LINQ queries, covering sorting, filtering, grouping and joins.

🔷 Part 1: Sorting

Sorting is a fundamental operation in data manipulation. LINQ provides a straightforward way to sort data in ascending or descending order.

➡️ Example 1: In this example, we're sorting a collection of numbers in ascending order. Observe how we're using the OrderBy method to simplify the sorting process.

using System;
using System.Linq;
using System.Collections.Generic;

public class Program{
    public static void Main(){
        var numbers = new List<int> { 5, 2, 8, 1, 9 };
        var sortedNumbers = numbers.OrderBy(n => n);
        foreach (var number in sortedNumbers){
            Console.WriteLine(number);
        }
    }
}

//Output: 1, 2, 5, 8, 9

➡️ Example 2: In this example, we will utilize the OrderByDescending method to arrange the provided list in a descending order.

using System;
using System.Linq;
using System.Collections.Generic;

public class Program{
    public static void Main(){
        var names = new List<string>{"Kramer", "Benes", "Seinfeld", "Costanza"};
        var reverseSortedNames = names.OrderByDescending(name => name);
        foreach (var name in reverseSortedNames){
            Console.WriteLine(name);
        }
    }
}

//Output: Seinfeld, Kramer, Costanza, Benes

➡️ Example 3: In this example, observe how we are using the Length property inside the OrderBy method to sort the list in ascending order based on the length of the individual items.

using System;
using System.Linq;
using System.Collections.Generic;

public class Program{
    public static void Main(){
        var fruits = new List<string>{"Apple", "Banana", "Grape", "Fig"};
        var customSortedFruits = fruits.OrderBy(fruit => fruit.Length);
        foreach (var fruit in customSortedFruits){
            Console.WriteLine(fruit);
        }
    }
}

//Output: Fig, Apple, Grape, Banana

🔷 Part 2: Filtering

LINQ offers simple and effective methods to filter data based on specific criteria.

➡️ Example 1: In this example, we are making use of the where clause to filter items that have the modulo of 2 as 0.

using System;
using System.Linq;
using System.Collections.Generic;

public class Program{
    public static void Main(){
        var numbers = new List<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        var evenNumbers = numbers.Where(n => n % 2 == 0);
        foreach (var number in evenNumbers){
            Console.WriteLine(number);
        }
    }
}

//Output: 2, 4, 6, 8, 10

➡️ Example 2: In this example, observe how we're using the where clause to filter items from a dictionary with more than one condition.

using System;
using System.Collections.Generic;
using System.Linq;

class Laptop{
    public string Brand { get; set; }
    public double Price { get; set; }
}

public class Program{
    public static void Main(){
        // Create a dictionary of laptops 
        var laptops = new Dictionary<string, Laptop>{{"Laptop1", new Laptop{Brand = "Apple", Price = 1500}}, {"Laptop2", new Laptop{Brand = "Dell", Price = 1200}}, {"Laptop3", new Laptop{Brand = "HP", Price = 800}}, {"Laptop4", new Laptop{Brand = "Lenovo", Price = 1100}}};
        // Use LINQ to filter expensive laptops from the dictionary 
        var expensiveLaptops = laptops.Where(kv => kv.Value.Price > 1000 && (kv.Value.Brand == "Apple" || kv.Value.Brand == "Dell"));
        // Display the filtered laptops 
        foreach (var laptop in expensiveLaptops){
            Console.WriteLine(laptop.Key + ": Brand - " + laptop.Value.Brand + ", Price - " + laptop.Value.Price);
        }
    }
}

/*
Laptop1: Brand - Apple, Price - 1500
Laptop2: Brand - Dell, Price - 1200
*/

🔷 Part 3: Grouping

LINQ offers an intuitive approach to group data based on specific criteria.

➡️ Example 1: In this example, observe how we're using the GroupBy function to group the students by grade.

using System;
using System.Collections.Generic;
using System.Linq;

class Student
{
    public string Name { get; set; }
    public int Grade { get; set; }
}

public class Program
{
    public static void Main()
    {
        List<Student> students = new List<Student>
        {
            new Student { Name = "Alice", Grade = 90 },
            new Student { Name = "Bob", Grade = 85 },
            new Student { Name = "Charlie", Grade = 90 },
            new Student { Name = "David", Grade = 78 },
            new Student { Name = "Eve", Grade = 85 }
        };

        // Group students by their grades
        var groupedByGrade = students.GroupBy(student => student.Grade);

        // Display the grouped data
        foreach (var group in groupedByGrade){
            Console.WriteLine("Students with grade " + group.Key + ":");
            foreach (var student in group){
                Console.WriteLine(student.Name);
            }
        }
    }
}

/*
Output:
Students with grade 90:
Alice
Charlie
Students with grade 85:
Bob
Eve
Students with grade 78:
David
*/

🔷 Part 4: Joins

LINQ provides an elegant method for combining data from multiple sources through joins.

➡️ Example 1: In this method, we're using the join method to combine and relate data from the students, courses, and enrollments collections, ultimately creating a result that shows which students are enrolled in which courses.

using System;
using System.Collections.Generic;
using System.Linq;

class Student{
    public int StudentId { get; set; }
    public string Name { get; set; }
}

class Course{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
}

class StudentCourse{
    public int StudentId { get; set; }
    public int CourseId { get; set; }
}

public class Program{
    public static void Main(){
        List<Student> students = new List<Student>{
            new Student { StudentId = 1, Name = "Alice" },
            new Student { StudentId = 2, Name = "Bob" },
            new Student { StudentId = 3, Name = "Charlie" },
        };

        List<Course> courses = new List<Course>{
            new Course { CourseId = 101, CourseName = "Math" },
            new Course { CourseId = 102, CourseName = "Science" },
            new Course { CourseId = 103, CourseName = "History" },
        };

        List<StudentCourse> enrollments = new List<StudentCourse>{
            new StudentCourse { StudentId = 1, CourseId = 101 },
            new StudentCourse { StudentId = 1, CourseId = 102 },
            new StudentCourse { StudentId = 2, CourseId = 101 },
        };

        var query = from student in students
                    join enrollment in enrollments
                    on student.StudentId equals enrollment.StudentId
                    join course in courses
                    on enrollment.CourseId equals course.CourseId
                    select new{
                        student.Name,
                        course.CourseName
                    };

        foreach (var result in query){
            Console.WriteLine(result.Name + " is enrolled in " + result.CourseName);
        }
    }
}

/*
Output:
Alice is enrolled in Math
Alice is enrolled in Science
Bob is enrolled in Math
*/

📌 Learn more about LINQ from Microsoft's documentation here.

Best Practices

Knowing and adhering to best practices is consistently important. Here are a few key considerations:

  • Using Meaningful Variable Names: Choose clear and meaningful variable names as they help to improve code readability and make it easier for others to understand the code.

  • Avoid Querying in a Loop: Refrain from placing LINQ queries inside loops. Instead, materialize the query into a collection first (using ToList) and then iterate through the collection. Querying in a loop can sometimes lead to performance issues.

  • Use Proper Exception Handling: LINQ queries can throw exceptions when dealing with data, such as null reference exceptions or index out-of-range exceptions. It's important to have appropriate error-handling mechanisms in place to gracefully manage and log exceptions.

  • Document and Comment Complex Queries: For complex LINQ queries, provide clear comments and documentation to explain the purpose and logic of the query. This helps other developers (and your future self) understand the code and maintain it effectively.

  • Understanding Deferred Execution: Deferred execution refers to the practice of delaying the actual execution of a query or operation on a data source until the result of the operation is specifically requested. This helps us optimize performance, reduce resource consumption, and create flexible, composable queries when working with data.

Conclusion

In conclusion, LINQ significantly streamlines data manipulation in .NET development. It empowers developers with efficient querying, filtering, and grouping of data, ultimately leading to cleaner, more expressive code, and enhancing overall software development practices.


Thanks for reading the article. Please give it a like and share it with your friends. Got any questions? Let me know!

Cheers 👋