C# tutorials > Language Integrated Query (LINQ) > LINQ to Objects > Using lambda expressions with LINQ

Using lambda expressions with LINQ

Using Lambda Expressions with LINQ in C#

LINQ (Language Integrated Query) provides a powerful and concise way to query data in C#. Lambda expressions are a crucial part of LINQ, allowing you to write inline, anonymous functions that are used in LINQ query methods. This tutorial will guide you through using lambda expressions with LINQ to Objects.

Introduction to Lambda Expressions in LINQ

Lambda expressions are essentially anonymous functions, or functions without a name. They are denoted by the => operator (the lambda operator), separating the input parameters on the left from the expression or statement block on the right.

The general syntax is:
(input parameters) => expression
or
(input parameters) => { statement block }

In the context of LINQ, lambda expressions are frequently used as arguments to standard query operators like Where, Select, OrderBy, etc.

Simple Filtering with Lambda Expressions

This example demonstrates filtering a list of integers to find only the even numbers. The Where method takes a lambda expression x => x % 2 == 0 as an argument. This lambda expression takes an integer x as input and returns true if x is even, and false otherwise. LINQ uses this to filter the numbers list, creating a new sequence containing only the even numbers.

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

public class Example
{
    public static void Main(string[] args)
    {
        List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        // Using lambda expression to filter even numbers
        IEnumerable<int> evenNumbers = numbers.Where(x => x % 2 == 0);

        Console.WriteLine("Even Numbers:");
        foreach (int num in evenNumbers)
        {
            Console.WriteLine(num);
        }
    }
}

Mapping with Lambda Expressions

This example uses the Select method to project each name in a list of strings to its length. The lambda expression name => name.Length takes a string name as input and returns its length. LINQ applies this lambda expression to each element in the names list, creating a new sequence containing the lengths of the names.

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

public class Example
{
    public static void Main(string[] args)
    {
        List<string> names = new List<string>() { "Alice", "Bob", "Charlie" };

        // Using lambda expression to project the length of each name
        IEnumerable<int> nameLengths = names.Select(name => name.Length);

        Console.WriteLine("Name Lengths:");
        foreach (int length in nameLengths)
        {
            Console.WriteLine(length);
        }
    }
}

Ordering with Lambda Expressions

This example uses the OrderBy method to sort a list of strings alphabetically. The lambda expression name => name takes a string name as input and returns the string itself, which is used as the key for sorting. LINQ uses this to sort the names list in ascending order.

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

public class Example
{
    public static void Main(string[] args)
    {
        List<string> names = new List<string>() { "Charlie", "Alice", "Bob" };

        // Using lambda expression to order names alphabetically
        IEnumerable<string> sortedNames = names.OrderBy(name => name);

        Console.WriteLine("Sorted Names:");
        foreach (string name in sortedNames)
        {
            Console.WriteLine(name);
        }
    }
}

Complex Filtering and Mapping

This example demonstrates a more complex scenario where we filter a list of Person objects based on their age and then select their names. The Where method uses the lambda expression person => person.Age > 28 to filter the list, and the Select method uses the lambda expression person => person.Name to extract the names of the filtered people.

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

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

public class Example
{
    public static void Main(string[] args)
    {
        List<Person> people = new List<Person>()
        {
            new Person { Name = "Alice", Age = 30 },
            new Person { Name = "Bob", Age = 25 },
            new Person { Name = "Charlie", Age = 35 }
        };

        // Using lambda expression to filter people older than 28 and select their names
        IEnumerable<string> olderPeopleNames = people
            .Where(person => person.Age > 28)
            .Select(person => person.Name);

        Console.WriteLine("Names of people older than 28:");
        foreach (string name in olderPeopleNames)
        {
            Console.WriteLine(name);
        }
    }
}

Concepts Behind the Snippet

The key concept is the use of lambda expressions as inline, anonymous functions that define the logic for filtering, mapping, and ordering data. LINQ methods such as Where, Select, and OrderBy accept delegates, and lambda expressions provide a concise way to create these delegates.

Real-Life Use Case

Imagine you have a database of products, and you need to find all products that are in stock and have a price greater than $50. You can use LINQ with lambda expressions to easily filter the product list based on these criteria.

Best Practices

  • Keep lambda expressions concise and readable.
  • Use meaningful variable names in the lambda expression.
  • Avoid complex logic within lambda expressions; refactor into separate methods if necessary.

Interview Tip

When asked about LINQ, be prepared to explain how lambda expressions are used to provide the filtering, mapping, and ordering logic. Demonstrate your understanding with simple examples.

When to Use Them

Use lambda expressions with LINQ when you need to perform filtering, mapping, ordering, or other data transformations on collections of objects in a concise and readable manner. They are particularly useful when the logic is simple and can be expressed in a single line.

Memory Footprint

Using LINQ with lambda expressions generally has a negligible impact on memory footprint for small to medium-sized collections. However, for very large datasets, consider the memory implications of intermediate collections created by LINQ operations. Using deferred execution (as LINQ does) can help minimize the memory footprint in many cases.

Alternatives

Alternatives to lambda expressions in LINQ include using named methods or anonymous delegates. However, lambda expressions are generally preferred for their conciseness and readability.

Pros

  • Concise syntax.
  • Improved readability.
  • Easy to use with LINQ query methods.

Cons

  • Can be difficult to debug if lambda expressions become too complex.
  • Overuse can lead to less readable code if not used judiciously.

FAQ

  • What is a lambda expression?

    A lambda expression is an anonymous function that can be treated as a value. It is typically used to define inline delegates without the need to explicitly declare a method.
  • How do I use lambda expressions with LINQ?

    Lambda expressions are passed as arguments to LINQ query methods such as Where, Select, and OrderBy. They define the logic for filtering, mapping, and ordering data.
  • Can I use multiple statements in a lambda expression?

    Yes, you can use multiple statements by enclosing them in curly braces {}. This creates a statement lambda, as opposed to an expression lambda.