Click here to Skip to main content
Click here to Skip to main content

Querying DataSet With LINQ in ASP.NET

By , 31 Jul 2012
 

Introduction

This article will demonstrate the basics for querying a DataSet with LINQ. We would be using XML as the datasource for this application.

Background

Basic understanding of ASP.NET, XML, DataSet and LINQ is desirable.

Using the code

XMLFile.xml (Our data source):

We would be using this XML file as the data source / database for our application.

<?xml version="1.0" encoding="utf-8" ?>
 
<DataSet>
 
  <Employee>
 
    <EmployeeID>1</EmployeeID>
 
    <Name>Sam</Name>
 
    <Age>25</Age>
 
  </Employee>
 
 
 
  <Employee>
 
    <EmployeeID>2</EmployeeID>
 
    <Name>Smith</Name>
 
    <Age>21</Age>
 
  </Employee>
 
 
 
  <Employee>
 
    <EmployeeID>3</EmployeeID>
 
    <Name>Miller</Name>
 
    <Age>32</Age>
 
  </Employee>
 
 
 
  <Employee>
 
    <EmployeeID>5</EmployeeID>
 
    <Name>Mark</Name>
 
    <Age>45</Age>
 
  </Employee>
 
</DataSet>

Default.aspx 

This is the ASPX file for our web application.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPNETWebApplication._Default" %>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
 
 
<head runat="server">
 
 
<title></title>
 
 
</head>
 
 
<body>
 
 
<form id="form1" runat="server">
 
 
<div>
 
 
</div>
 
 
</form>
 
 
</body>
 
</html> 

Default.aspx.cs

This is our source (C#) file for the application. 

           
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
namespace ASPNETWebApplication
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            // Read XML into the DataSet
            System.Data.DataSet ds = new System.Data.DataSet();
            ds.ReadXml(Server.MapPath("~/XMLFile.xml"));
 
            // Execute LINQ
            var query = from a in ds.Tables[0].AsEnumerable()
                        where a.Field<string>("Name") == "Miller"
                        select a;
 
            // Display records from the query
            foreach (var EmpID in query)
            {
                Response.Write(EmpID["Name"] + "'s Employee ID - " + EmpID["EmployeeID"].ToString());
            }
        }
    }
} 
 

Explanation

Here we firstly declare and object of DataSet and then we read the XML file into the Data Set.

Here our objective is to find the Employee ID of Employee named "Miller" from the DataSet using LINQ.

Then we execute a LINQ query and get the results into the "query" variable. As the result is in the form of a resultset, for individually getting the records we declare an Object EmpID of type query and loop through the records to get the results.

History 

Next update would include more about DataSet and LINQ.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

HemendraSingh88
Software Developer Symphony Services, Bangalore
India India
Member
I am a software developer working mainly on .NET and SQL Server.
 
My Website: www.hemendrasingh.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 31 Jul 2012
Article Copyright 2012 by HemendraSingh88
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid