---
title: "Introduction to LINQ in C# with Functions"  
description: "C# using readable, concise, and SQL-like syntax. It works on arrays, lists, databases, XML, and more. With LINQ, you can filter, sort, group, and tran"  
author: "Anubhav Sharma"  
published: 2025-04-22  
updated: 2025-04-22  
canonical: https://www.mindstick.com/articles/339105/introduction-to-linq-in-c-sharp-with-functions  
category: "c#"  
tags: ["c#", "linq"]  
reading_time: 4 minutes  

---

# Introduction to LINQ in C# with Functions

**LINQ** ([Language Integrated](https://www.mindstick.com/forum/159866/what-are-the-advantages-of-using-linq-language-integrated-query-in-c-sharp) Query) lets you query [collections in C#](https://www.mindstick.com/articles/12458/what-are-collections-in-c-sharp) using readable, concise, and SQL-like syntax. It works on arrays, lists, databases, XML, and more. With LINQ, you can filter, sort, group, and transform data using built-in **functions**.

## LINQ Syntax Styles

C# offers two main styles for writing LINQ:

| Syntax Type | Example |
| --- | --- |
| **Query Syntax** | `from n in numbers where n > 5 select n` |
| **Method Syntax** | `numbers.Where(n => n > 5)` |

## Common LINQ Functions

Here’s a list of LINQ functions you’ll use most often, with short examples:

## 1. Where – Filter items by condition

```cs
var evenNumbers = numbers.Where(n => n % 2 == 0);
```

## 2. Select – Transform each item

```cs
var squares = numbers.Select(n => n * n);
```

## 3. OrderBy / OrderByDescending – Sort items

```cs
var sorted = numbers.OrderBy(n => n);             // Ascending
var desc = numbers.OrderByDescending(n => n);     // Descending
```

## 4. First / FirstOrDefault – Get the first item

```cs
var firstEven = numbers.First(n => n % 2 == 0);
var orDefault = numbers.FirstOrDefault(n => n > 10); // Returns default (0 for int) if not found
```

## 5. Any / All – Boolean checks

```cs
bool hasEven = numbers.Any(n => n % 2 == 0);
bool allPositive = numbers.All(n => n > 0);
```

## 6. Count / Sum / Max / Min / Average

```cs
int count = numbers.Count();
int sum = numbers.Sum();
int max = numbers.Max();
double avg = numbers.Average();
```

**7. Distinct – [Remove duplicates](https://www.mindstick.com/forum/160918/help-with-writing-a-query-to-remove-duplicates-from-a-table-in-sql-server)**

```cs
var unique = numbers.Distinct();
```

## 8. Take / Skip

```cs
var first3 = numbers.Take(3); // First 3 items
var skip2 = numbers.Skip(2);  // Skip first 2
```

## 9. GroupBy – Group items by key

```cs
var grouped = names.GroupBy(n => n[0]);
foreach (var group in grouped)
{
    Console.WriteLine($"Group: {group.Key}");
    foreach (var name in group)
        Console.WriteLine(name);
}
```

## 10. Join – Join two collections

```cs
var result = students.Join(
    grades,
    student => student.Id,
    grade => grade.StudentId,
    (student, grade) => new { student.Name, grade.Score }
);
```

## Practical Example

```cs
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] numbers = { 5, 2, 9, 1, 5, 6 };

        var query = numbers
                    .Where(n => n > 3)
                    .Distinct()
                    .OrderBy(n => n)
                    .Select(n => $"Number: {n}");

        foreach (var item in query)
        {
            Console.WriteLine(item);
        }
    }
}
```

## Output:

```plaintext
Number: 5
Number: 6
Number: 9
```

## LINQ Method Cheat Sheet

| Method | Purpose |
| --- | --- |
| `Where()` | Filter based on condition |
| `Select()` | Project/transform each element |
| `OrderBy()` | Sort ascending |
| `OrderByDescending()` | Sort descending |
| `First()` | Get the first [matching element](https://www.mindstick.com/forum/158053/how-to-remove-the-matching-element-of-the-array-in-javascript) |
| `FirstOrDefault()` | Return default if not found |
| `Any()` | Check if any match exists |
| `All()` | Check if all match condition |
| `Count()` | Count elements |
| `Sum()` | Add all values |
| `Distinct()` | Remove duplicates |
| `Take(n)` | Take first `n` elements |
| `Skip(n)` | Skip first `n` elements |
| `GroupBy()` | Group elements by a key |
| `Join()` | Combine two data sources by key |

## Summary

| Topic | Description |
| --- | --- |
| What is LINQ | A [query language](https://www.mindstick.com/forum/158386/what-is-the-purpose-of-the-sqlite-query-language-and-how-does-it-differ-from-other-sql-dialects) integrated in C# |
| Works With | Arrays, Lists, XML, SQL, JSON |
| Styles | Query and Method Syntax |
| Functions | Powerful methods like `Where`, `Select`, `GroupBy` |
| Benefits | Clean, readable, [maintainable code](https://answers.mindstick.com/qa/111596/what-are-the-best-practices-for-writing-clean-and-maintainable-code) |

## Next Steps

1. Learn about **deferred execution**
2. Explore **[LINQ to SQL](https://www.mindstick.com/forum/12665/linq-to-sql-array-list-insertion)** or **[Entity Framework](https://www.mindstick.com/forum/12875/how-to-use-object-query-with-a-where-and-to-retrieve-entity-record-entity-framework)**
3. Try writing **[LINQ queries](https://www.mindstick.com/forum/159878/what-is-the-role-of-the-join-keyword-in-linq-queries) with [complex data](https://answers.mindstick.com/qa/35624/what-type-of-language-do-you-prefer-for-writing-complex-data-structures) types**

---

Original Source: https://www.mindstick.com/articles/339105/introduction-to-linq-in-c-sharp-with-functions

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
