codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

39 / 60 topics
38LINQ in C#39LINQ to Objects40LINQ to SQL
Tutorials/C# Programming/LINQ to Objects
🔷C# Programming

LINQ to Objects

Updated 2026-05-15
10 min read

LINQ to Objects

Introduction

Language Integrated Query (LINQ) is a powerful feature in C# that allows developers to write queries against collections of objects in a concise and expressive manner. LINQ to Objects enables you to query any IEnumerable<T> collection, such as lists, arrays, or dictionaries, using a syntax similar to SQL.

In this tutorial, we will explore how to use LINQ to query and manipulate data within collections. We'll cover basic querying techniques, filtering, sorting, grouping, and transforming data.

Concept

LINQ provides a set of standard query operators that can be used to perform various operations on collections. These operators are available in the System.Linq namespace and include methods like Where, Select, OrderBy, GroupBy, and many more.

Key LINQ Operators

  • Where: Filters elements based on a predicate.
  • Select: Projects each element of a sequence into a new form.
  • OrderBy: Sorts the elements of a sequence in ascending order.
  • GroupBy: Groups elements by a specified key.
  • Aggregate: Applies an accumulator function over a sequence.

Examples

Let's dive into some practical examples to understand how these operators work.

Basic Querying

Suppose we have a list of integers and we want to find all numbers greater than 10.

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

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 5, 12, 8, 13, 6, 9, 11 };

        var result = from num in numbers
                     where num > 10
                     select num;

        foreach (var num in result)
        {
            Console.WriteLine(num);
        }
    }
}

<OutputBlock>
{`12  
13  
11`}
</OutputBlock>

In this example, the `Where` clause filters the numbers greater than 10, and the `Select` clause projects each number into a new form (in this case, the same number).

### Using Method Syntax

LINQ also supports method syntax, which can be more readable for complex queries.

```csharp
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 5, 12, 8, 13, 6, 9, 11 };

        var result = numbers.Where(num => num > 10);

        foreach (var num in result)
        {
            Console.WriteLine(num);
        }
    }
}

<OutputBlock>
{`12  
13  
11`}
</OutputBlock>

### Sorting

To sort a collection, you can use the `OrderBy` method.

```csharp
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David" };

        var sortedNames = from name in names
                         orderby name.Length descending
                         select name;

        foreach (var name in sortedNames)
        {
            Console.WriteLine(name);
        }
    }
}

<OutputBlock>
{`Charlie  
Alice  
David  
Bob`}
</OutputBlock>

In this example, the `OrderBy` clause sorts the names by their length in descending order.

### Grouping

To group elements by a key, you can use the `GroupBy` method.

```csharp
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<string> words = new List<string> { "apple", "banana", "cherry", "date", "elderberry" };

        var groupedWords = from word in words
                          group word by word[0] into g
                          select new { FirstLetter = g.Key, Words = g };

        foreach (var group in groupedWords)
        {
            Console.WriteLine($"First Letter: {group.FirstLetter}");
            foreach (var word in group.Words)
            {
                Console.WriteLine(word);
            }
        }
    }
}
Output
First Letter: a  
apple  
First Letter: b  
banana  
First Letter: c  
cherry  
First Letter: d  
date  
First Letter: e  
elderberry

In this example, the GroupBy clause groups words by their first letter.

Transforming Data

To transform data, you can use the Select method to project each element into a new form.

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

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

        var squares = from num in numbers
                     select num * num;

        foreach (var square in squares)
        {
            Console.WriteLine(square);
        }
    }
}
Output
1  
4  
9  
16  
25

In this example, the Select clause projects each number into its square.

What's Next?

Now that you have a good understanding of LINQ to Objects, you can explore more advanced topics such as LINQ to SQL. LINQ to SQL allows you to query and manipulate data in relational databases using LINQ syntax, providing a seamless integration between your C# code and the database.

Stay tuned for more tutorials on querying and manipulating data with LINQ!


PreviousLINQ in C#Next LINQ to SQL

Recommended Gear

LINQ in C#LINQ to SQL