Wednesday, April 30, 2008



This blog post summarizes the overview of LINQ.

Below are the new features been introduced in Orcas

§ Automatic Properties

§ Object Initializers

§ Collection Initializers

§ Extesnion Methods

§ Lambda Expressions - p => expressions

§ Query Syntax

§ Anonymous Types

§ Var Keyword

These language features help in making query against relational data. This overall querying programming model is called as "LINQ" - which stands for .NET Language Integrated Query.

Note: LINQ supports a rich extensibility model that facilitates the creation of efficient domain-specific providers for data sources. .NET 3.5 ships with built-in libraries that enable LINQ support against Objects, XML, and Databases.


· LINQ to XML is a built-in LINQ Data Provider that is implemented within the "System.Xml.Linq" namespace in .NET 3.5.

· LINQ to XML provides a clean programming model that enables you to Read, Construct And Write XML data.

· LINQ to XML provides a really powerful way to efficiently query, filter, and shape/transform XML data.

· We can use LINQ to XML to perform LINQ queries over XML that either retrieved from the file-system, from any In-Memory XML content or from a remote HTTP URL or web-service.

· i.e. We can use it both against local XML content, as well as remote XML feeds.

· LINQ to XML provides much richer (and easier) querying and data shaping support than the low-level XmlReader/XmlWriter API in .NET today.

· It also ends up being much more efficient (and uses much less memory) than the DOM API that XmlDocument provides.

· LINQ to XML can be used to easily transform XML data into .NET objects and collections that you can further manipulate and transfer across your application.

· LINQ uses same Query Syntax And Concepts when querying data for all below.

1. LINQ to XML

2. LINQ to SQL

3. LINQ to Objects

4. LINQ to SharePoint

5. LINQ to Amazon

6. LINQ to NHibernate, etc.

LINQ to XML - Query a local XML File

Suppose we have a simple XML file as below.





<Institution>DDD Denmark</Institution>





Below is the code - Use the new XDocument class within the System.Xml.Linq namespace to open and query the XML document above

private void ReadXML()


XDocument tableXML = XDocument.Load(Server.MapPath(XmlFileName1));

var employees = from employee in tableXML.Descendants("Employee")

where employee.Attribute("status").Value == "Y"

select new


CourseID = employee.Element("CourseId").Value,

Sequence = employee.Element("Sequence").Value,

YearOfPassing = employee.Element("YearOfPassing").Value,

Institution = employee.Element("Institution").Value,

Course = employee.Element("Course").Value,

Average = (int?) employee.Element("Average") ?? 0 // Notice the use of C# ?? null coalescing operator


GridView1.DataSource = employees;



LINQ to XML - Query a Remote RSS Feed

private void ReadRemoteRSS()


XNamespace slashNamespace = Namespace;

XDocument tableXML = XDocument.Load(RSSFeed);

var posts = from item in tableXML.Descendants("item")

select new


Title = item.Element("title").Value,

Published = DateTime.Parse(item.Element("pubDate").Value),

Url = item.Element("link").Value,

NumComments = item.Element(slashNamespace + "comments").Value,


// Notice- Further sub-query is used on the result to filter the latest posts

var newposts = from item in posts

where (DateTime.Now - item.Published).Days < 7

select item;

GridView1.DataSource = posts.ToList();




Hope this helps.

Thanks & Regards,

Arun Manglick || Tech Lead

No comments:

Post a Comment