C# LINQ标准查询操作符
Posted 指间的徘徊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# LINQ标准查询操作符相关的知识,希望对你有一定的参考价值。
首先添加数据集合
1 [Serializable] 2 public class Racer : IComparable<Racer>, IFormattable 3 { 4 public Racer() 5 { } 6 public Racer(string firstName, string lastName, string country, int starts, int wins) 7 : this(firstName, lastName, country, starts, wins, null, null) 8 { 9 } 10 public Racer(string firstName, string lastName, string country, int starts, int wins, IEnumerable<int> years, IEnumerable<string> cars) 11 { 12 this.FirstName = firstName; 13 this.LastName = lastName; 14 this.Country = country; 15 this.Starts = starts; 16 this.Wins = wins; 17 this.Years = new List<int>(years); 18 this.Cars = new List<string>(cars); 19 } 20 public string FirstName { get; set; } 21 public string LastName { get; set; } 22 public string Country { get; set; } 23 public int Wins { get; set; } 24 public int Starts { get; set; } 25 public IEnumerable<string> Cars { get; private set; } 26 public IEnumerable<int> Years { get; private set; } 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 return string.Compare(this.LastName, other.LastName); 37 } 38 39 public string ToString(string format) 40 { 41 return ToString(format, null); 42 } 43 44 public string ToString(string format, 45 IFormatProvider formatProvider) 46 { 47 switch (format) 48 { 49 case null: 50 case "N": 51 return ToString(); 52 case "F": 53 return FirstName; 54 case "L": 55 return LastName; 56 case "C": 57 return Country; 58 case "S": 59 return Starts.ToString(); 60 case "W": 61 return Wins.ToString(); 62 case "A": 63 return String.Format("{0} {1}, {2}; starts: {3}, wins: {4}", 64 FirstName, LastName, Country, Starts, Wins); 65 default: 66 throw new FormatException(String.Format("Format {0} not supported", format)); 67 } 68 } 69 }
1 private static List<Racer> racers; 2 3 public static IList<Racer> GetChampions() 4 { 5 if (racers == null) 6 { 7 racers = new List<Racer>(40); 8 racers.Add(new Racer("Nino", "Farina", "Italy", 33, 5, new int[] { 1950 }, new string[] { "Alfa Romeo" })); 9 racers.Add(new Racer("Alberto", "Ascari", "Italy", 32, 10, new int[] { 1952, 1953 }, new string[] { "Ferrari" })); 10 racers.Add(new Racer("Juan Manuel", "Fangio", "Argentina", 51, 24, new int[] { 1951, 1954, 1955, 1956, 1957 }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" })); 11 racers.Add(new Racer("Mike", "Hawthorn", "UK", 45, 3, new int[] { 1958 }, new string[] { "Ferrari" })); 12 racers.Add(new Racer("Phil", "Hill", "USA", 48, 3, new int[] { 1961 }, new string[] { "Ferrari" })); 13 racers.Add(new Racer("John", "Surtees", "UK", 111, 6, new int[] { 1964 }, new string[] { "Ferrari" })); 14 racers.Add(new Racer("Jim", "Clark", "UK", 72, 25, new int[] { 1963, 1965 }, new string[] { "Lotus" })); 15 racers.Add(new Racer("Jack", "Brabham", "Australia", 125, 14, new int[] { 1959, 1960, 1966 }, new string[] { "Cooper", "Brabham" })); 16 racers.Add(new Racer("Denny", "Hulme", "New Zealand", 112, 8, new int[] { 1967 }, new string[] { "Brabham" })); 17 racers.Add(new Racer("Graham", "Hill", "UK", 176, 14, new int[] { 1962, 1968 }, new string[] { "BRM", "Lotus" })); 18 racers.Add(new Racer("Jochen", "Rindt", "Austria", 60, 6, new int[] { 1970 }, new string[] { "Lotus" })); 19 racers.Add(new Racer("Jackie", "Stewart", "UK", 99, 27, new int[] { 1969, 1971, 1973 }, new string[] { "Matra", "Tyrrell" })); 20 racers.Add(new Racer("Emerson", "Fittipaldi", "Brazil", 143, 14, new int[] { 1972, 1974 }, new string[] { "Lotus", "McLaren" })); 21 racers.Add(new Racer("James", "Hunt", "UK", 91, 10, new int[] { 1976 }, new string[] { "McLaren" })); 22 racers.Add(new Racer("Mario", "Andretti", "USA", 128, 12, new int[] { 1978 }, new string[] { "Lotus" })); 23 racers.Add(new Racer("Jody", "Scheckter", "South Africa", 112, 10, new int[] { 1979 }, new string[] { "Ferrari" })); 24 racers.Add(new Racer("Alan", "Jones", "Australia", 115, 12, new int[] { 1980 }, new string[] { "Williams" })); 25 racers.Add(new Racer("Keke", "Rosberg", "Finland", 114, 5, new int[] { 1982 }, new string[] { "Williams" })); 26 racers.Add(new Racer("Niki", "Lauda", "Austria", 173, 25, new int[] { 1975, 1977, 1984 }, new string[] { "Ferrari", "McLaren" })); 27 racers.Add(new Racer("Nelson", "Piquet", "Brazil", 204, 23, new int[] { 1981, 1983, 1987 }, new string[] { "Brabham", "Williams" })); 28 racers.Add(new Racer("Ayrton", "Senna", "Brazil", 161, 41, new int[] { 1988, 1990, 1991 }, new string[] { "McLaren" })); 29 racers.Add(new Racer("Nigel", "Mansell", "UK", 187, 31, new int[] { 1992 }, new string[] { "Williams" })); 30 racers.Add(new Racer("Alain", "Prost", "France", 197, 51, new int[] { 1985, 1986, 1989, 1993 }, new string[] { "McLaren", "Williams" })); 31 racers.Add(new Racer("Damon", "Hill", "UK", 114, 22, new int[] { 1996 }, new string[] { "Williams" })); 32 racers.Add(new Racer("Jacques", "Villeneuve", "Canada", 165, 11, new int[] { 1997 }, new string[] { "Williams" })); 33 racers.Add(new Racer("Mika", "Hakkinen", "Finland", 160, 20, new int[] { 1998, 1999 }, new string[] { "McLaren" })); 34 racers.Add(new Racer("Michael", "Schumacher", "Germany", 287, 91, new int[] { 1994, 1995, 2000, 2001, 2002, 2003, 2004 }, new string[] { "Benetton", "Ferrari" })); 35 racers.Add(new Racer("Fernando", "Alonso", "Spain", 177, 27, new int[] { 2005, 2006 }, new string[] { "Renault" })); 36 racers.Add(new Racer("Kimi", "Räikkönen", "Finland", 148, 17, new int[] { 2007 }, new string[] { "Ferrari" })); 37 racers.Add(new Racer("Lewis", "Hamilton", "UK", 90, 17, new int[] { 2008 }, new string[] { "McLaren" })); 38 racers.Add(new Racer("Jenson", "Button", "UK", 208, 12, new int[] { 2009 }, new string[] { "Brawn GP" })); 39 racers.Add(new Racer("Sebastian", "Vettel", "Germany", 81, 21, new int[] { 2010, 2011 }, new string[] { "Red Bull Racing" })); 40 } 41 42 return racers; 43 }
1、where子句
1 var racers = from r in Formula1.GetChampions() 2 where r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria") 3 select r; 4 var item = Formula1.GetChampions().Where(p => p.Wins > 15 && (p.Country == "Brazil" || p.Country == "Austria")).Select(r => r);
2、根据索引筛选
1 var racers = Formula1.GetChampions(). 2 Where((r, index) => r.LastName.StartsWith("A") && index % 2 != 0);
3、类型筛选
1 object[] data = { "one", 2, 3, "four", "five", 6 }; 2 var query = data.OfType<string>();
4、复合的from子句
1 var ferrariDrivers = from r in Formula1.GetChampions() 2 from c in r.Cars 3 where c == "Ferrari" 4 orderby r.LastName 5 select r.FirstName + " " + r.LastName; 6 var item = Formula1.GetChampions().SelectMany(r => r.Cars, (r, c) => new { Racer = r, Car = c }). 7 Where(r => r.Car == "Ferrari").OrderBy(r => r.Racer.LastName).Select(r => r.Racer.FirstName + "" + r.Racer.LastName);
5、排序
1 var racers = from r in Formula1.GetChampions() 2 where r.Country == "Brazil" 3 orderby r.Wins descending 4 select r; 5 var racers2= from r in Formula1.GetChampions() 6 orderby r.Country,r.LastName,r.FirstName 7 select r; 8 C#数据操作LINQ