LINQ Queries in C# – Basics and Examples

IHUB Talent – The Best Full Stack .NET Training Institute in Hyderabad with Live Internship Program

If you're planning to build a rewarding career in software development, then IHUB is the perfect place to begin. Recognized as the best Full Stack .NET training institute in Hyderabad, IHUB offers a complete job-oriented course with a live intensive internship program, guided by real-time industry experts.

Whether you're a graduate, postgraduate, someone with an educational gap, or planning a job domain change, IHUB’s Full Stack .NET course is designed to make you job-ready in just a few months. The program is structured for freshers and working professionals alike, offering hands-on experience and placement support.

LINQ Queries in C# – Basics and Examples

Language Integrated Query (LINQ) is one of the most powerful and expressive features introduced in C#. It allows developers to write queries directly within C# code to retrieve and manipulate data from various sources such as arrays, collections, XML, SQL databases, and more. LINQ simplifies data querying, making the code more readable and concise.

What is LINQ?

LINQ stands for Language Integrated Query. It bridges the gap between programming languages and data sources. With LINQ, developers can use the same syntax and pattern to query different types of data. LINQ is part of the System.Linq namespace in .NET.

Types of LINQ

C# supports various types of LINQ:

LINQ to Objects – for collections like arrays, lists

LINQ to SQL – for querying SQL Server databases

LINQ to XML – for working with XML data

LINQ to Entities – used with Entity Framework

Basic Syntax

LINQ offers two syntaxes:

Query Syntax (SQL-like)

Method Syntax (Lambda expressions)

Examples of LINQ Queries

Let’s consider a simple list of integers:

csharp

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

1. Query Syntax

csharp

var evenNumbers = from num in numbers

                  where num % 2 == 0

                  select num;

foreach (var n in evenNumbers)

{

    Console.WriteLine(n);

}

2. Method Syntax

csharp

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

foreach (var n in evenNumbers)

{

    Console.WriteLine(n);

}

Working with Custom Objects

csharp

public class Student

{

    public int Id { get; set; }

    public string Name { get; set; }

}

List<Student> students = new List<Student>

{

    new Student { Id = 1, Name = "John" },

    new Student { Id = 2, Name = "Alice" },

    new Student { Id = 3, Name = "Bob" }

};

var result = from s in students

             where s.Name.StartsWith("A")

             select s;

foreach (var student in result)

{

    Console.WriteLine(student.Name);

}

Conclusion

LINQ in C# makes data manipulation simple, elegant, and consistent across various data sources. Whether you're filtering a list or querying a database, LINQ offers a powerful way to write clean and readable code. Learning the basics of LINQ is essential for every C# developer aiming to write efficient and maintainable applications. 

Read More

Dependency Injection in ASP.NET Core

RESTful API Development using ASP.NET Core Web API

Frontend Validation in ASP.NET

Visit Our I-HUB Talent Training Institute in Hyderabad

Comments

Popular posts from this blog

Why Choose .NET for Full Stack Development?

What is Full Stack .NET Development?

What is ASP.NET Core?