Projection
[Blog Map] [Table of Contents] [Next Topic]
Projection is the abstraction of taking one shape of data and creating a different shape of it. For instance, you could take a collection of one type, filter it and/or sort it, and then project a collection of a new type.
This blog is inactive.
New blog: EricWhite.com/blog
Blog TOCThe Select extension method is what you use to implement projection on a collection. You can pass a lambda expression to the Select extension method that takes one object from the source collection, and projects one object into the new projection.
Let's say that we have a collection of integers, and we want a new collection of strings of x's, where the string lengths are determined by the source collection of integers. The following code shows how to do this:
int[] lengths =
new[] { 1, 3, 5, 7, 9, 7, 5, 3, 1 };
IEnumerable<string> strings =
lengths.Select(i => "".PadRight(i, 'x'));
foreach (var s in strings)
Console.WriteLine(s);
This code produces the following output:
x
xxx
xxxxx
xxxxxxx
xxxxxxxxx
xxxxxxx
xxxxx
xxx
x
Or we could take an array of doubles, and project a collection of integers, casting each double to int:
double[] dbls =
new[] { 1.5, 3.2, 5.2, 7.3, 9.7, 7.5, 5.0, 3.2, 1.9 };
IEnumerable<int> ints =
dbls.Select(d => (int)d);
foreach (var i in ints)
Console.WriteLine(i);
Or we can take a collection of elements from an XML tree, and project a collection of strings. The strings projection contains the value of each element:
XElement xmlDoc = XElement.Parse(
@"<Root>
<Child>abc</Child>
<Child>def</Child>
<Child>ghi</Child>
</Root>");
IEnumerable<string> values =
xmlDoc.Elements().Select(e => (string)e);
foreach (string s in values)
Console.WriteLine(s);
But in the real world, our projections will typically be much more involved than this. For instance, we will often take a collection of some class, and project a collection of a different class. Or we may project a collection of anonymous types (introduced in the next topic).
[Blog Map] [Table of Contents] [Next Topic]
Comments
Anonymous
February 24, 2010
The comment has been removedAnonymous
May 04, 2010
I wanted to see what that last piece of code produced, and got a compile error: "Error 6 A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else"Anonymous
May 12, 2010
I find very interesting output IEnumerable<XElement> values = xmlDoc.Elements(); foreach (XElement xe in values) Console.WriteLine(xe.ToString); why the output is defferent from using "(string)xe" as parameter of the writeline method.Anonymous
May 12, 2010
I got the answer in the documentation of the XElement class :) // // Summary: // Cast the value of this System.Xml.Linq.XElement to a System.String. // // Parameters: // element: // The System.Xml.Linq.XElement to cast to System.String. // // Returns: // A System.String that contains the content of this System.Xml.Linq.XElement. [CLSCompliant(false)] public static explicit operator string(XElement element);Anonymous
January 03, 2014
Thanks, also need 101 Examples with data from an XML file rather than a list.