Exploring LINQ Query Syntax and Methods

Tutorial 3 of 5

Introduction

In this tutorial, we will dive into the world of LINQ (Language Integrated Query), a powerful feature in C# that allows us to work with data in a more efficient and readable way. We will focus on understanding the two types of LINQ syntax: Query and Method. By the end of this tutorial, you will have a solid understanding of how to construct and utilize LINQ queries using both syntaxes and know their key differences.

What you will learn

  • The basic concepts of LINQ
  • How to construct LINQ queries using Query Syntax
  • How to construct LINQ queries using Method Syntax
  • Differences between Query and Method Syntax

Prerequisites

  • Basic knowledge of C#
  • Familiarity with .NET Framework

Step-by-Step Guide

LINQ provides a language-agnostic way to query data. There are two ways to write LINQ queries: Query Syntax and Method Syntax.

Query Syntax is similar to SQL and is easy to read and understand. It is often used for querying in-memory collections like Lists and Arrays.

Method Syntax (also known as Fluent) uses extension methods included in the Enumerable or Queryable static class. It provides more functionality compared to Query Syntax.

Code Examples

Query Syntax

Here is a simple example of a LINQ query using Query Syntax:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

// Query Syntax
var evenNumbers = from num in numbers
                  where num % 2 == 0
                  select num;

// print the result
foreach (var num in evenNumbers)
{
    Console.WriteLine(num);
}

In the above example, from num in numbers defines the data source, where num % 2 == 0 applies the filter condition, and select num specifies the selection.

The output will be:

2
4

Method Syntax

The same query can be written using Method Syntax as follows:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

// Method Syntax
var evenNumbers = numbers.Where(num => num % 2 == 0);

// print the result
foreach (var num in evenNumbers)
{
    Console.WriteLine(num);
}

In Method Syntax, we use the Where extension method to filter the numbers. The expression num => num % 2 == 0 is a lambda expression that defines the filter condition.

The output will be the same as the previous example.

Summary

This tutorial introduced you to LINQ, and its two syntaxes: Query and Method. You learned how to construct LINQ queries and understand their differences.

Moving forward, you can explore more advanced LINQ operators such as sorting, grouping, joining, etc.

Practice Exercises

  1. Using both Query and Method Syntax, write a LINQ query that selects all the words in a string array that start with the letter 'a'.

  2. Using both Query and Method Syntax, write a LINQ query that finds the maximum number in an array of integers.

Solutions

  1. Query for words that start with 'a'
string[] words = { "apple", "banana", "avocado", "cherry", "apricot" };

// Query Syntax
var result = from word in words
             where word.StartsWith("a")
             select word;

// Method Syntax
var result2 = words.Where(word => word.StartsWith("a"));
  1. Query for maximum number
int[] numbers = { 1, 2, 3, 4, 5 };

// Query Syntax
var maxNumber = (from num in numbers
                 select num).Max();

// Method Syntax
var maxNumber2 = numbers.Max();

Remember to practice and experiment with different queries to get a better understanding of LINQ. Happy Coding!