[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:

Blog TOC
The 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)

This code produces the following output:


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)

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(
IEnumerable<string> values =
    xmlDoc.Elements().Select(e => (string)e);
foreach (string s in values)

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 (5)

  1. Reihani says:

    Thank you

    very nice !

    I add this blog to my favorites

    God Speed you.

  2. Judy says:

    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"

  3. wind says:

    I find very interesting output

    IEnumerable<XElement> values =


           foreach (XElement xe  in values)


    why the output is defferent from using  "(string)xe" as parameter of the writeline method.

  4. wind says:

    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.


           public static explicit operator string(XElement element);

  5. Dinesh says:

    Thanks, also need 101 Examples with data from an XML file rather than a list.

Skip to main content