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.