๐ ASP.NET โ LINQ Integration โ Query Data Easily in Web Forms (With Full Code Explanation)
๐งฒ Introduction โ What Is LINQ in ASP.NET?
LINQ (Language Integrated Query) in ASP.NET allows you to write SQL-like queries directly in C# to interact with in-memory collections, databases, XML, and more.
It integrates naturally into Web Forms and is widely used with:
- Lists & Arrays
- DataTables
- Entity Framework / LINQ to SQL
- XML files
๐ฏ In this guide, you’ll learn:
- How to use LINQ to query collections and DataTables in ASP.NET
- How to bind query results to controls like
GridViewandRepeater - Syntax explanations and browser outputs
- Full step-by-step breakdowns for beginners
๐ LINQ Integration โ File Overview
| File Name | Purpose |
|---|---|
LinqToObjects.aspx | LINQ on List (in-memory collection) |
LinqToDataTable.aspx | LINQ on DataTable |
LinqIntegration.aspx.cs | Code-behind with LINQ logic |
๐ Example 1: LINQ on In-Memory List (List<T>)
โ
Markup File โ LinqToObjects.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LinqToObjects.aspx.cs" Inherits="LinqToObjects" %>
<html>
<body>
<form id="form1" runat="server">
<h3>๐ LINQ to List<T></h3>
<asp:GridView ID="GridView1" runat="server" />
</form>
</body>
</html>
โ๏ธ Code-Behind โ LinqToObjects.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
public partial class LinqToObjects : System.Web.UI.Page
{
public class Product
{
public int Id;
public string Name;
public double Price;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Product> products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 750.00 },
new Product { Id = 2, Name = "Mouse", Price = 25.99 },
new Product { Id = 3, Name = "Monitor", Price = 150.50 }
};
var expensiveItems = from p in products
where p.Price > 50
select p;
GridView1.DataSource = expensiveItems;
GridView1.DataBind();
}
}
}
๐ Code Explanation
List<Product>: Creates a strongly typed in-memory list.- LINQ Query:
from p in products where p.Price > 50 select pfilters products over $50. GridView1.DataSource: Assigns the LINQ result to the GridView.DataBind(): Renders the filtered result in the browser.
๐ Example 2: LINQ to DataTable
โ
Markup File โ LinqToDataTable.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LinqToDataTable.aspx.cs" Inherits="LinqToDataTable" %>
<html>
<body>
<form id="form1" runat="server">
<h3>๐ LINQ to DataTable</h3>
<asp:GridView ID="GridView1" runat="server" />
</form>
</body>
</html>
โ๏ธ Code-Behind โ LinqToDataTable.aspx.cs
using System;
using System.Data;
using System.Linq;
public partial class LinqToDataTable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Marks", typeof(int));
dt.Rows.Add("Alice", 78);
dt.Rows.Add("Bob", 90);
dt.Rows.Add("Carol", 65);
var topStudents = from row in dt.AsEnumerable()
where row.Field<int>("Marks") >= 70
select new
{
Name = row.Field<string>("Name"),
Marks = row.Field<int>("Marks")
};
GridView1.DataSource = topStudents.ToList();
GridView1.DataBind();
}
}
}
๐ Code Explanation
DataTable dt: Creates a virtual in-memory data table.dt.Rows.Add(...): Adds mock student data.dt.AsEnumerable(): Enables LINQ querying on the DataTable.- LINQ filters rows with Marks โฅ 70.
- Result is bound and displayed in the GridView.
๐ Summary โ Recap & Takeaways
- LINQ simplifies querying collections and tables in C#.
- Works well with in-memory data like
List<T>andDataTable. - Helps reduce code complexity while improving readability.
- You can easily bind filtered data to controls like GridView.
๐ Key Takeaways:
- Use LINQ
from ... where ... selectfor clear data queries. - Works great for filtering, sorting, and shaping data.
- Combine with
DataBind()to connect results to UI.
โ LINQ in ASP.NET is ideal for building dynamic, filterable web pages.
โ Frequently Asked Questions (FAQs)
โ Whatโs the benefit of LINQ over loops?
โ
LINQ provides more concise, readable, and declarative syntax for filtering and projecting data.
โ Can I use LINQ with databases?
โ
Yes, using LINQ to SQL or Entity Framework for database queries.
โ How do I debug a LINQ query?
โ
You can convert the LINQ result to a list (ToList()) and inspect it in Visual Studio.
โ What if I need dynamic filtering?
โ
You can use where conditions with variables or use lambda expressions.
Share Now :
