Using LINQ to XML (and how to build a custom RSS Feed Reader with it)
page 6 of 9
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 39006/ 91

Using LINQ Sub-Queries within a LINQ to XML Query Expression

If you look at the raw XML of my RSS feed, you'll notice that the "tag" comments for each post are stored as repeated <category> elements directly below each <item> element:

Figure 13

When designing the object model for a "BlogEntry" class, I might want to represent these <category> values as a sub-collection of strings.  For example, using a "Tags" property that is a generic list of type string:

Figure 14

You might be wondering - how do we take a flat collection of <category> elements under <item> and transform them into a nested sub-collection of strings?  The nice thing about LINQ is that it makes this type of scenario easy by allowing us to use nested LINQ query expressions like so:

Figure 15

This "shaping" power of LINQ, and its ability to take flat data structures and make them hierarchical (and take hierarchical data structures and make them flat) is super powerful.  You can use this feature with any type of data source - regardless of whether it is XML, SQL, or plain old objects/arrays/collections.


View Entire Article

User Comments

Title: Using LINQ Sub-Queries within a LINQ to XML Query Expression   
Name: Ken Jinks
Date: 2009-07-10 1:27:32 PM
Comment:
How would you incorporate a where clause into the subquery such that you only retrieve those items with a particular TAG value?

thanks
kenneth.jinks@qinetiq-na.com
Title: How to integrate with XLINQ in eixsting applications   
Name: Senthil
Date: 2008-12-17 12:21:13 AM
Comment:
How to integrate with XLINQ in eixsting applications






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-04 8:58:19 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search