1. Introduction
C# has evolved to make developers’ lives easier — and Lambda Expressions and LINQ (Language Integrated Query) are two powerful tools that simplify how we write, read, and manage data.
In traditional ASP.NET Web Forms, you may have written lengthy code to:
Filter a list of data
Search records by condition
Sort or group collections
With LINQ and Lambda Expressions, all these tasks can be done in one line of code 🚀.
2. What Are Lambda Expressions?
A Lambda Expression is a short, inline way to write anonymous methods.
Syntax
(parameters) => expression_or_statement
Example
x => x * x
This means: “Take input x, and return x * x.”
3. What Is LINQ?
LINQ (Language Integrated Query) allows querying collections, arrays, or databases in C# directly — using SQL-like syntax.
Example
var result = from n in numbers
where n > 50
select n;
Or using Lambda
var result = numbers.Where(n => n > 50);
LINQ works with:
Arrays and Lists (
System.Linq)DataTables
Entity Framework (Database LINQ)
XML data
4. Real-Time Example in ASP.NET Web Forms
Let’s create a web page that displays a product list and allows users to search products by name or price range — all powered by LINQ and Lambda.
Step 1: Create ASPX Page – ProductList.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductList.aspx.cs" Inherits="ProductList" %>
<!DOCTYPE html>
<html>
<head>
<title>LINQ and Lambda in ASP.NET WebForms</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:600px; margin:auto; text-align:center; font-family:Arial;">
<h2> Product Search using LINQ & Lambda</h2>
<asp:Label ID="lblSearch" runat="server" Text="Enter Product Name or Price Range:"></asp:Label><br />
<asp:TextBox ID="txtSearch" runat="server" Width="250px"></asp:TextBox><br /><br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /><br /><br />
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="true"
BorderColor="#ccc" BorderStyle="Solid" BorderWidth="1px" />
</div>
</form>
</body>
</html>
Step 2: Code Behind – ProductList.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
public partial class ProductList : System.Web.UI.Page
{
// Sample Product Class
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
// Create static product list
List<Product> products = new List<Product>()
{
new Product(){ Id=1, Name="Laptop", Price=55000 },
new Product(){ Id=2, Name="Mouse", Price=500 },
new Product(){ Id=3, Name="Keyboard", Price=1200 },
new Product(){ Id=4, Name="Monitor", Price=8000 },
new Product(){ Id=5, Name="Headphones", Price=1500 },
new Product(){ Id=6, Name="Smartphone", Price=25000 }
};
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvProducts.DataSource = products;
gvProducts.DataBind();
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
string searchValue = txtSearch.Text.Trim().ToLower();
// Using Lambda + LINQ for filtering
var filteredProducts = products.Where(p =>
p.Name.ToLower().Contains(searchValue) ||
p.Price.ToString().Contains(searchValue)
).ToList();
gvProducts.DataSource = filteredProducts;
gvProducts.DataBind();
}
}
5. Output Example
Before Search:
All products are listed in a grid.
User Enters “mouse”:
Grid shows only “Mouse” record.

Join the conversation! Your thoughts help the community grow.