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.

268 ❘ ChaPTer 11 lAnGuAGe inteGrAted Query<br />

IFormattable to support different variants of format strings, <strong>and</strong> the interface IComparable,<br />

which can be used to sort a list of racers based on the LastName. For doing more advanced queries, the<br />

class Racer contains not only single value properties such as FirstName, LastName, Wins, Country, <strong>and</strong><br />

Starts, but also multivalue properties such as Cars <strong>and</strong> Years. The Years property lists all the years of the<br />

championship title. Some racers have won more than one title. The Cars property is used to list all the cars<br />

that have been used by the driver during the title years.<br />

using System;<br />

using System.Text;<br />

namespace Wrox.ProCSharp.LINQ<br />

{<br />

[Serializable]<br />

public class Racer: IComparable, IFormattable<br />

{<br />

public Racer(string firstName = null, string lastName = null,<br />

string country = null, int starts = 0, int wins = 0,<br />

IEnumerable years = null, IEnumerable cars = null)<br />

{<br />

this.FirstName = firstName;<br />

this.LastName = lastName;<br />

this.Country = country;<br />

this.Starts = starts;<br />

this.Wins = wins;<br />

var yearsList = new List();<br />

foreach (var year in years)<br />

{<br />

yearsList.Add(year);<br />

}<br />

this.Years = yearsList.ToArray();<br />

var carList = new List();<br />

foreach (var car in cars)<br />

{<br />

carList.Add(car);<br />

}<br />

this.Cars = carList.ToArray();<br />

}<br />

public string FirstName {get; set;}<br />

public string LastName {get; set;}<br />

public int Wins {get; set;}<br />

public string Country {get; set;}<br />

public int Starts {get; set;}<br />

public string[] Cars { get; private set; }<br />

public int[] Years { get; private set; }<br />

public override string ToString()<br />

{<br />

return String.Format("{0} {1}", FirstName, LastName);<br />

}<br />

public int CompareTo(Racer other)<br />

{<br />

if (other == null) throw new ArgumentNullException("other");<br />

return this.LastName.CompareTo(other.LastName);<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!