标签:style blog io ar color os 使用 sp for
1 public class Racer : IComparable<Racer>, IFormattable 2 { 3 public string FirstName { get; set; } 4 public string LastName { get; set; } 5 public int Wins { get; set; } 6 public string Country { get; set; } 7 public int Starts { get; set; } 8 public IEnumerable<string> Cars { get; set; } 9 public IEnumerable<int> Years { get; set; } 10 11 public Racer(string firstName,string lastName,string country,int wins,int starts,IEnumerable<int> years,IEnumerable<string> cars) 12 { 13 FirstName = firstName; 14 LastName = lastName; 15 Wins = wins; 16 Country = country; 17 Starts = starts; 18 Cars = new List<string>(cars); 19 Years = new List<int>(years); 20 } 21 22 public Racer(string firstName, string lastName, int wins, string country, int starts) 23 : this(firstName, lastName,country, wins, starts,null, null) 24 { 25 26 } 27 28 public override string ToString() 29 { 30 return string.Format("{0}_{1}", FirstName, LastName); 31 } 32 33 public int CompareTo(Racer other) 34 { 35 if (other == null) return -1; 36 int result = string.Compare(FirstName, other.FirstName); 37 if (result == 0) 38 result = string.Compare(LastName, other.LastName); 39 return result; 40 } 41 42 public string ToString(string format, IFormatProvider formatProvider) 43 { 44 format = format ?? "N"; 45 switch(format) 46 { 47 case "N": 48 return ToString(); 49 case "C": 50 return string.Format("{0} Country:{1}", ToString(), Country); 51 case "S": 52 return string.Format("{0} Starts:{1}", ToString(), Starts); 53 case "W": 54 return string.Format("{0} Wins:{1}", ToString(), Wins); 55 case "Y": 56 var result=ToString(); 57 foreach(var item in Years) 58 { 59 result += item; 60 } 61 return result; 62 default: 63 throw new FormatException(string.Format("Format {0} not supported", format)); 64 } 65 } 66 }
1 public class Team 2 { 3 public string Name { get; private set; } 4 public IEnumerable<int> Years { get; private set; } 5 6 public Team(string name,params int[] years) 7 { 8 Name = name; 9 Years = new List<int>(years); 10 } 11 }
1 public static class Formula1 2 { 3 private static List<Racer> racers; 4 private static List<Team> teams; 5 6 public static List<Racer> Racers 7 { 8 get { return racers ?? (racers = new List<Racer>(40) { new Racer("Nino", "Farina", "Italy", 33, 5, new[] { 1950 }, new[] { "Alfa Romeo" }), new Racer("Alberto", "Ascari", "Italy", 30, 10, new[] { 1952, 1953 }, new[] { "Ferrari" }), new Racer("Juan Manuel", "Fangio", "Argentina", 51, 24, new[] { 1951, 1954, 1955, 1956, 1957 }, new[] { "Alfa Romeo", "Maserati" }), new Racer("Mike", "Hawthorn", "UK", 45, 3, new[] { 1958 }, new[] { "Ferrari" }) }); } 9 } 10 11 public static List<Team> Teams 12 { 13 get 14 { 15 return teams ?? (teams = new List<Team>{new Team("Vanwall",1958), 16 new Team("Cooper", 1959, 1960), 17 new Team("Ferrari", 1961, 1964, 1975, 1976, 1977, 1979, 1982,1983, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008), 18 new Team("BRM", 1962), 19 new Team("Lotus", 1963, 1965, 1968, 1970, 1972, 1973, 1978), 20 new Team("Brabham", 1966, 1967), 21 new Team("Matra", 1969), 22 new Team("Tyrrell", 1971)}); 23 } 24 } 25 }
1 static void Main(string[] args) 2 { 3 var result = from r in Formula1.Racers 4 from c in r.Cars 5 where c.Equals("Ferrari") 6 orderby r.FirstName 7 select r.FirstName + " " + r.LastName; 8 9 foreach(var item in result) 10 { 11 Console.WriteLine(item); 12 } 13 14 var result2 = Formula1.Racers.SelectMany(r => r.Cars, (r, c) => new { Racer = r, Car = c }) 15 .Where(r => r.Car.Equals("Ferrari")).OrderBy(r => r.Racer.FirstName).Select(r => r.Racer.FirstName + " " + r.Racer.LastName); 16 17 Console.ReadKey(); 18 }
标签:style blog io ar color os 使用 sp for
原文地址:http://www.cnblogs.com/darknoll/p/4146468.html