13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Extension methods <strong>in</strong> .NET 3.5267as a delegate. It’s very like the ConvertAll method <strong>in</strong> List, but operat<strong>in</strong>g on anyenumerable collection and us<strong>in</strong>g deferred execution to perform the projection only aseach element is requested.When I <strong>in</strong>troduced anonymous types, I said they were useful with lambda expressionsand LINQ—well, here’s an example of the k<strong>in</strong>d of th<strong>in</strong>g you can do with them.We’ve currently got the odd numbers between 0 and 9 (<strong>in</strong> reverse order)—let’s createa type that encapsulates the square root of the number as well as the orig<strong>in</strong>al number.List<strong>in</strong>g 10.9 shows both the projection and a slightly modified way of writ<strong>in</strong>g out theresults. I’ve adjusted the whitespace solely for the sake of space on the pr<strong>in</strong>ted page.List<strong>in</strong>g 10.9Projection us<strong>in</strong>g a lambda expression and an anonymous typevar collection = Enumerable.Range(0, 10).Where(x => x%2 != 0).Reverse().Select(x => new { Orig<strong>in</strong>al=x, SquareRoot=Math.Sqrt(x) } );foreach (var element <strong>in</strong> collection){Console.WriteL<strong>in</strong>e("sqrt({0})={1}",element.Orig<strong>in</strong>al,element.SquareRoot);}This time the type of collection isn’t IEnumerable—it’s IEnumerable,where Someth<strong>in</strong>g is the anonymous type created by the compiler. We can’texplicitly type the collection variable except as either the nongeneric IEnumerabletype or object. Implicit typ<strong>in</strong>g is what allows us to use the Orig<strong>in</strong>al and SquareRootproperties when writ<strong>in</strong>g out the results. The output of list<strong>in</strong>g 10.9 is as follows:sqrt(9)=3sqrt(7)=2.64575131106459sqrt(5)=2.23606797749979sqrt(3)=1.73205080756888sqrt(1)=1Of course, a Select method doesn’t have to use an anonymous type at all—we couldhave selected just the square root of the number, discard<strong>in</strong>g the orig<strong>in</strong>al. In that casethe result would have been IEnumerable. Alternatively, we could have manuallywritten a type to encapsulate an <strong>in</strong>teger and its square root—it was just easiest touse an anonymous type <strong>in</strong> this case.Let’s look at one last method to round off our coverage of Enumerable for themoment: OrderBy.10.3.4 Sort<strong>in</strong>g us<strong>in</strong>g the OrderBy methodSort<strong>in</strong>g data is a common requirement when process<strong>in</strong>g data, and <strong>in</strong> LINQ this is usuallyperformed us<strong>in</strong>g the OrderBy or OrderByDescend<strong>in</strong>g methods, sometimes followedby ThenBy or ThenByDescend<strong>in</strong>g if you need to sort by more than one propertyof the data. This ability to sort on multiple properties has always been available theLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!