๐Ÿ—ƒ๏ธ ASP.NET Data Handling & Integration
Estimated reading: 4 minutes 62 views

๐Ÿ“Š 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 GridView and Repeater
  • Syntax explanations and browser outputs
  • Full step-by-step breakdowns for beginners

๐Ÿ“‚ LINQ Integration โ€“ File Overview

File NamePurpose
LinqToObjects.aspxLINQ on List (in-memory collection)
LinqToDataTable.aspxLINQ on DataTable
LinqIntegration.aspx.csCode-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 p filters 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> and DataTable.
  • Helps reduce code complexity while improving readability.
  • You can easily bind filtered data to controls like GridView.

๐Ÿ” Key Takeaways:

  • Use LINQ from ... where ... select for 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“Š ASP.NET โ€“ LINQ Integration

Or Copy Link

CONTENTS
Scroll to Top