🔍 LINQ and Lambda Expressions in .NET: A Complete Guide with Real Examples


📘 Introduction
In modern .NET development, two tools stand out for their ability to make code cleaner, more expressive, and functional: LINQ (Language Integrated Query) and Lambda Expressions. Together, they allow you to query, transform, and manipulate data elegantly and efficiently—whether in lists, databases, XML files, or APIs.
In this article, you'll learn how LINQ and Lambda work, how to use them step by step, explore practical and advanced examples, and discover their main benefits and limitations.
🧠 What is LINQ?
LINQ is a feature introduced in .NET Framework 3.5 that allows you to perform queries directly on collections, databases, XML, and other data formats using syntax integrated into the C# language.
Types of LINQ:
LINQ to Objects – for in-memory collections (lists, arrays)
LINQ to SQL – for querying SQL Server databases
LINQ to XML – for manipulating XML documents
LINQ to Entities – used with Entity Framework
🧮 What are Lambda Expressions?
Lambda Expressions are anonymous functions that allow you to write short, functional expressions. They're often used with LINQ to define filtering, sorting, and projection criteria.
Basic syntax:
(parameters) => expression
Example:
x => x > 10
🛠️ LINQ with Lambda: Step by Step
1. Creating a data collection
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
2. Filtering with LINQ + Lambda
var evens = numbers.Where(x => x % 2 == 0);
3. Sorting elements
var sorted = numbers.OrderByDescending(x => x);
4. Projecting data (Select)
var squares = numbers.Select(x => x * x);
5. Combining multiple operations
var result = numbers
.Where(x => x > 5)
.Select(x => x * 2)
.OrderBy(x => x);
🔧 Advanced Examples
📋 Filtering with multiple conditions
List<string> names = new List<string> { "Ana", "Bruno", "Carlos", "Amanda", "Beatriz" };
var filteredNames = names.Where(n => n.StartsWith("A") && n.Length > 3);
🔢 Aggregation with Sum
, Average
, Max
, Min
List<int> sales = new List<int> { 100, 200, 150, 300 };
int total = sales.Sum();
double average = sales.Average();
🧮 Grouping with GroupBy
var products = new List<(string Category, string Name)>
{
("Electronics", "TV"),
("Electronics", "Phone"),
("Furniture", "Sofa"),
("Furniture", "Table")
};
var grouped = products.GroupBy(p => p.Category);
🔗 Joining lists with Join
var customers = new List<(int Id, string Name)>
{
(1, "John"),
(2, "Mary"),
(3, "Peter")
};
var orders = new List<(int CustomerId, string Product)>
{
(1, "Laptop"),
(2, "Smartphone"),
(1, "Mouse")
};
var query = customers.Join(
orders,
customer => customer.Id,
order => order.CustomerId,
(customer, order) => new { customer.Name, order.Product }
);
🧑💼 Working with complex objects
class Employee
{
public string Name { get; set; }
public string Department { get; set; }
public double Salary { get; set; }
}
var employees = new List<Employee>
{
new Employee { Name = "Lucas", Department = "IT", Salary = 5000 },
new Employee { Name = "Fernanda", Department = "HR", Salary = 4000 },
new Employee { Name = "Carlos", Department = "IT", Salary = 5500 }
};
var seniorIT = employees
.Where(e => e.Department == "IT" && e.Salary > 5000)
.Select(e => e.Name);
🧹 Removing duplicates with Distinct
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
var distinct = numbers.Distinct();
🔄 Pagination with Skip
and Take
List<string> items = Enumerable.Range(1, 100).Select(i => $"Item {i}").ToList();
var page2 = items.Skip(10).Take(10);
✅ Benefits
Readability: Cleaner and more expressive code
Productivity: Fewer lines of code for common tasks
Flexibility: Works with various data sources
Integration: Fully integrated with C# and .NET
❌ Drawbacks
Performance: Can be less efficient with large datasets if misused
Debugging: Harder to debug complex expressions
Complexity: May confuse beginners with functional syntax
Runtime errors: Some operations fail only at runtime, not at compile time
📌 Conclusion
LINQ and Lambda Expressions are essential tools for any modern .NET developer. They offer an elegant and functional approach to data manipulation but require attention to performance and code clarity. Mastering these techniques is a key step toward writing more efficient, readable, and professional applications
#DotNet #CSharp #LINQ #LambdaExpressions #BackendDev #CleanCode #FunctionalProgramming #DevTips #SoftwareEngineering
Subscribe to my newsletter
Read articles from Johnny Hideki Kinoshita de Faria directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Johnny Hideki Kinoshita de Faria
Johnny Hideki Kinoshita de Faria
Technology professional with over 15 years of experience delivering innovative, scalable, and secure solutions — especially within the financial sector. I bring deep expertise in Oracle PL/SQL (9+ years), designing robust data architectures that ensure performance and reliability. On the back-end side, I’ve spent 6 years building enterprise-grade applications using .NET, applying best practices like TDD and clean code to deliver high-quality solutions. In addition to my backend strengths, I have 6 years of experience with PHP and JavaScript, allowing me to develop full-stack web applications that combine strong performance with intuitive user interfaces. I've led and contributed to projects involving digital account management, integration of VISA credit and debit transactions, modernization of payment systems, financial analysis tools, and fraud prevention strategies. Academically, I hold a postgraduate certificate in .NET Architecture and an MBA in IT Project Management, blending technical skill with business acumen. Over the past 6 years, I’ve also taken on leadership roles — managing teams, mentoring developers, and driving strategic initiatives. I'm fluent in agile methodologies and make consistent use of tools like Azure Boards to coordinate tasks and align team performance with delivery goals.