15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

284 ❘ ChaPTer 11 lAnGuAGe inteGrAted Query<br />

requires a string parameter <strong>and</strong> returns IEnumerable < Racer > , similar to the method that was implemented<br />

before. For doing this, several generic Func < > delegates are defi ned, so you do not need to declare your<br />

own delegate. A Lambda expression is assigned to the variable racersByCar . The left side of the Lambda<br />

expression defi nes a car variable of the type that is the fi rst generic parameter of the Func delegate (a<br />

string). The right side defi nes the LINQ query that uses the parameter with the where clause:<br />

Func < string, IEnumerable < Racer > > racersByCar =<br />

car = > from r in Formula1.GetChampions()<br />

from c in r.Cars<br />

where c == car<br />

orderby r.LastName<br />

select r;<br />

Now you can use the Intersect() extension method to get all racers that won the championship with a<br />

Ferrari <strong>and</strong> a McLaren:<br />

Console.WriteLine("World champion with Ferrari <strong>and</strong> McLaren");<br />

foreach (var racer in racersByCar("Ferrari").Intersect(<br />

racersByCar("McLaren")))<br />

{<br />

Console.WriteLine(racer);<br />

}<br />

The result is just one racer, Niki Lauda:<br />

World champion with Ferrari <strong>and</strong> McLaren<br />

Niki Lauda<br />

The Set operations compares the objects by invoking the GetHashCode() <strong>and</strong><br />

Equals() method of the entity class. For custom comparision, you can also pass an<br />

object that implements the interface IEqualityComparer < T > . In the sample here,<br />

the GetChampions() method always returns the same objects <strong>and</strong> thus the default<br />

comparison works.<br />

zip<br />

The Zip() method is new with .<strong>NET</strong> 4 <strong>and</strong> enables you to merge two related sequences into one with a<br />

predicate function.<br />

First, two related sequences are created, both with the same fi ltering (country Italy) <strong>and</strong> ordering. For<br />

merging this is important, as item 1 from the fi rst collection is merged with item 1 from the second<br />

collection, item 2 with item 2, <strong>and</strong> so on. In case the count of the two sequences is different, Zip() just<br />

stops when the end of the smaller collection is reached.<br />

The items in the fi rst collection have a Name property <strong>and</strong> the items in the second collection have LastName<br />

<strong>and</strong> Starts properties.<br />

Using the Zip() method on the collection racerNames requires the second collection racerNamesAndStarts<br />

as the fi rst parameter. The second parameter is of type Func < TFirst, TSecond, TResult > . This<br />

parameter is implemented as a Lambda expression <strong>and</strong> receives the elements of the fi rst collection with the<br />

parameter first , <strong>and</strong> the elements of the second collection with the parameter second . The implementation<br />

creates <strong>and</strong> returns a string containing the Name property of the fi rst element <strong>and</strong> the Starts property of the<br />

second element:<br />

var racerNames = from r in Formula1.GetChampions()<br />

where r.Country == "Italy"<br />

orderby r.Wins descending<br />

select new<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!