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

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

LINQ in C#

Updated 2026-05-15
10 min read

LINQ in C#

Introduction

Language Integrated Query (LINQ) is a powerful feature introduced by Microsoft that allows developers to write queries against various data sources using a single, consistent syntax. LINQ provides a way to query and manipulate data from different types of collections such as arrays, lists, databases, XML documents, and more. This tutorial will introduce you to the basics of LINQ in C# and how it can simplify your code when dealing with data.

Concept

LINQ stands for Language Integrated Query, which means that querying is integrated into the language itself. Before LINQ, developers had to use different methods or libraries to query various types of data sources. LINQ unifies these approaches by providing a common syntax and set of operators that can be used across different data sources.

Key Features of LINQ

  1. Unified Syntax: LINQ provides a consistent syntax for querying different data sources, making it easier to learn and use.
  2. Type Safety: LINQ queries are type-safe because they are written using C# syntax.
  3. Deferred Execution: LINQ queries are not executed immediately when they are defined. Instead, they are deferred until the results are actually needed. This can improve performance by allowing for more efficient query execution.
  4. Extensibility: LINQ can be extended to support custom data sources and operations.

Examples

Let's start with some practical examples to understand how LINQ works in C#.

Example 1: Querying a List of Integers

Suppose you have a list of integers, and you want to find all the even numbers greater than 5. Here’s how you can do it using LINQ:

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5class Program
6{
7 static void Main()
8 {
9 List<int> numbers = new List<int> { 1, 3, 6, 8, 9, 10, 12 };
10
11 var query = from num in numbers
12 where num > 5 && num % 2 == 0
13 select num;
14
15 foreach (var number in query)
16 {
17 Console.WriteLine(number);
18 }
19 }
20}

In this example:

  • We define a list of integers.
  • We create a LINQ query using the from, where, and select keywords.
  • The foreach loop is used to iterate over the results of the query.
Output
6
8
10
12

Example 2: Querying an Array of Strings

Now, let's see how you can use LINQ to query an array of strings. Suppose you have an array of names and you want to find all the names that start with the letter "J":

csharp
1using System;
2using System.Linq;
3
4class Program
5{
6 static void Main()
7 {
8 string[] names = { "John", "Jane", "Alice", "Jack", "Bob" };
9
10 var query = from name in names
11 where name.StartsWith("J")
12 select name;
13
14 foreach (var name in query)
15 {
16 Console.WriteLine(name);
17 }
18 }
19}

In this example:

  • We define an array of strings.
  • We create a LINQ query to find names that start with "J".
  • The foreach loop is used to print the results.
Output
John
Jane
Jack

Example 3: Querying a List of Objects

LINQ can also be used to query lists of objects. Suppose you have a list of Person objects and you want to find all people who are older than 25:

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5class Person
6{
7 public string Name { get; set; }
8 public int Age { get; set; }
9}
10
11class Program
12{
13 static void Main()
14 {
15 List<Person> people = new List<Person>
16 {
17 new Person { Name = "Alice", Age = 28 },
18 new Person { Name = "Bob", Age = 22 },
19 new Person { Name = "Charlie", Age = 30 }
20 };
21
22 var query = from person in people
23 where person.Age > 25
24 select person;
25
26 foreach (var person in query)
27 {
28 Console.WriteLine($"{person.Name} is {person.Age} years old.");
29 }
30 }
31}

In this example:

  • We define a Person class with properties Name and Age.
  • We create a list of Person objects.
  • We use LINQ to query the list for people older than 25.
  • The results are printed using string interpolation.
Output
Alice is 28 years old.
Charlie is 30 years old.

What's Next?

In this tutorial, we introduced you to the basics of LINQ in C# and how it can be used to query different types of data sources. In the next section, we will explore "LINQ to Objects," which allows you to perform queries on collections such as lists and arrays.

By mastering LINQ, you will be able to write more concise and readable code when working with data in C#.


PreviousStack in C#Next LINQ to Objects

Recommended Gear

Stack in C#LINQ to Objects