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

Hmm - What is this "anonymous type" thing?

In my code above I've taken advantage of a new language feature in VB and C# called "anonymous types".  Anonymous types enable developers to concisely define inline CLR types within code, without having to explictly define a formal class declaration of the type.  You can learn more about them in my previous New "Orcas" Language Feature: Anonymous Types blog post.

While anonymous types can be super useful when you want to locally iterate and work with data, we'll often want/need to define a standard class when passing the results of our LINQ query between multiple classes, across class library assemblies, and over web-services. 

To enable this, I could define a non-anonymous class called "FeedDefinition" to represent our Feed data like so:

Figure 8

Note above how I'm using the new "Automatic Properties" feature of C# to define the properties (and avoid having to define a field for them).

I could then write the below method to return back a generics based List<FeedDefinition> collection containing FeedDefinition objects:

Figure 9

Note above how the only change I've made to the LINQ to XML query we were using before is to change the "select" clause from "select new" (with no type-name) to "select new FeedDefinition".  With this change I'm now returning a sequence of FeedDefinition objects that I can pass from class to class, assembly to assembly, and across web-services.


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-11-29 1:32:32 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search