首页 / C# / C# LINQ标准查询操作符
C# LINQ标准查询操作符
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# LINQ标准查询操作符,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9144字,纯文字阅读大概需要14分钟。
内容图文
首先添加数据集合
1 [Serializable] 2 public class Racer : IComparable<Racer>, IFormattable 3 { 4public Racer() 5 { } 6public Racer(string firstName, string lastName, string country, int starts, int wins) 7 : this(firstName, lastName, country, starts, wins, null, null) 8 { 9 } 10public Racer(string firstName, string lastName, string country, int starts, int wins, IEnumerable<int> years, IEnumerable<string> cars) 11 { 12this.FirstName = firstName; 13this.LastName = lastName; 14this.Country = country; 15this.Starts = starts; 16this.Wins = wins; 17this.Years = new List<int>(years); 18this.Cars = new List<string>(cars); 19 } 20publicstring FirstName { get; set; } 21publicstring LastName { get; set; } 22publicstring Country { get; set; } 23publicint Wins { get; set; } 24publicint Starts { get; set; } 25public IEnumerable<string> Cars { get; privateset; } 26public IEnumerable<int> Years { get; privateset; } 2728publicoverridestring ToString() 29 { 30return String.Format("{0} {1}", FirstName, LastName); 31 } 3233publicint CompareTo(Racer other) 34 { 35if (other == null) return -1; 36returnstring.Compare(this.LastName, other.LastName); 37 } 3839publicstring ToString(string format) 40 { 41return ToString(format, null); 42 } 4344publicstring ToString(string format, 45 IFormatProvider formatProvider) 46 { 47switch (format) 48 { 49casenull: 50case"N": 51return ToString(); 52case"F": 53return FirstName; 54case"L": 55return LastName; 56case"C": 57return Country; 58case"S": 59return Starts.ToString(); 60case"W": 61return Wins.ToString(); 62case"A": 63return String.Format("{0} {1}, {2}; starts: {3}, wins: {4}", 64 FirstName, LastName, Country, Starts, Wins); 65default: 66thrownew FormatException(String.Format("Format {0} not supported", format)); 67 } 68 } 69 }
1 private static List<Racer> racers; 2 3publicstatic IList<Racer> GetChampions() 4 { 5if (racers == null) 6 { 7 racers = new List<Racer>(40); 8 racers.Add(new Racer("Nino", "Farina", "Italy", 33, 5, newint[] { 1950 }, newstring[] { "Alfa Romeo" })); 9 racers.Add(new Racer("Alberto", "Ascari", "Italy", 32, 10, newint[] { 1952, 1953 }, newstring[] { "Ferrari" })); 10 racers.Add(new Racer("Juan Manuel", "Fangio", "Argentina", 51, 24, newint[] { 1951, 1954, 1955, 1956, 1957 }, newstring[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" })); 11 racers.Add(new Racer("Mike", "Hawthorn", "UK", 45, 3, newint[] { 1958 }, newstring[] { "Ferrari" })); 12 racers.Add(new Racer("Phil", "Hill", "USA", 48, 3, newint[] { 1961 }, newstring[] { "Ferrari" })); 13 racers.Add(new Racer("John", "Surtees", "UK", 111, 6, newint[] { 1964 }, newstring[] { "Ferrari" })); 14 racers.Add(new Racer("Jim", "Clark", "UK", 72, 25, newint[] { 1963, 1965 }, newstring[] { "Lotus" })); 15 racers.Add(new Racer("Jack", "Brabham", "Australia", 125, 14, newint[] { 1959, 1960, 1966 }, newstring[] { "Cooper", "Brabham" })); 16 racers.Add(new Racer("Denny", "Hulme", "New Zealand", 112, 8, newint[] { 1967 }, newstring[] { "Brabham" })); 17 racers.Add(new Racer("Graham", "Hill", "UK", 176, 14, newint[] { 1962, 1968 }, newstring[] { "BRM", "Lotus" })); 18 racers.Add(new Racer("Jochen", "Rindt", "Austria", 60, 6, newint[] { 1970 }, newstring[] { "Lotus" })); 19 racers.Add(new Racer("Jackie", "Stewart", "UK", 99, 27, newint[] { 1969, 1971, 1973 }, newstring[] { "Matra", "Tyrrell" })); 20 racers.Add(new Racer("Emerson", "Fittipaldi", "Brazil", 143, 14, newint[] { 1972, 1974 }, newstring[] { "Lotus", "McLaren" })); 21 racers.Add(new Racer("James", "Hunt", "UK", 91, 10, newint[] { 1976 }, newstring[] { "McLaren" })); 22 racers.Add(new Racer("Mario", "Andretti", "USA", 128, 12, newint[] { 1978 }, newstring[] { "Lotus" })); 23 racers.Add(new Racer("Jody", "Scheckter", "South Africa", 112, 10, newint[] { 1979 }, newstring[] { "Ferrari" })); 24 racers.Add(new Racer("Alan", "Jones", "Australia", 115, 12, newint[] { 1980 }, newstring[] { "Williams" })); 25 racers.Add(new Racer("Keke", "Rosberg", "Finland", 114, 5, newint[] { 1982 }, newstring[] { "Williams" })); 26 racers.Add(new Racer("Niki", "Lauda", "Austria", 173, 25, newint[] { 1975, 1977, 1984 }, newstring[] { "Ferrari", "McLaren" })); 27 racers.Add(new Racer("Nelson", "Piquet", "Brazil", 204, 23, newint[] { 1981, 1983, 1987 }, newstring[] { "Brabham", "Williams" })); 28 racers.Add(new Racer("Ayrton", "Senna", "Brazil", 161, 41, newint[] { 1988, 1990, 1991 }, newstring[] { "McLaren" })); 29 racers.Add(new Racer("Nigel", "Mansell", "UK", 187, 31, newint[] { 1992 }, newstring[] { "Williams" })); 30 racers.Add(new Racer("Alain", "Prost", "France", 197, 51, newint[] { 1985, 1986, 1989, 1993 }, newstring[] { "McLaren", "Williams" })); 31 racers.Add(new Racer("Damon", "Hill", "UK", 114, 22, newint[] { 1996 }, newstring[] { "Williams" })); 32 racers.Add(new Racer("Jacques", "Villeneuve", "Canada", 165, 11, newint[] { 1997 }, newstring[] { "Williams" })); 33 racers.Add(new Racer("Mika", "Hakkinen", "Finland", 160, 20, newint[] { 1998, 1999 }, newstring[] { "McLaren" })); 34 racers.Add(new Racer("Michael", "Schumacher", "Germany", 287, 91, newint[] { 1994, 1995, 2000, 2001, 2002, 2003, 2004 }, newstring[] { "Benetton", "Ferrari" })); 35 racers.Add(new Racer("Fernando", "Alonso", "Spain", 177, 27, newint[] { 2005, 2006 }, newstring[] { "Renault" })); 36 racers.Add(new Racer("Kimi", "R?ikk?nen", "Finland", 148, 17, newint[] { 2007 }, newstring[] { "Ferrari" })); 37 racers.Add(new Racer("Lewis", "Hamilton", "UK", 90, 17, newint[] { 2008 }, newstring[] { "McLaren" })); 38 racers.Add(new Racer("Jenson", "Button", "UK", 208, 12, newint[] { 2009 }, newstring[] { "Brawn GP" })); 39 racers.Add(new Racer("Sebastian", "Vettel", "Germany", 81, 21, newint[] { 2010, 2011 }, newstring[] { "Red Bull Racing" })); 40 } 4142return racers; 43 }
1、where子句
1 var racers = from r in Formula1.GetChampions() 2where r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria") 3select r; 4var 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 }; 2var query = data.OfType<string>();
4、复合的from子句
1 var ferrariDrivers = from r in Formula1.GetChampions() 2from c in r.Cars 3where c == "Ferrari"4orderby r.LastName 5select r.FirstName + "" + r.LastName; 6var 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() 2where r.Country == "Brazil"3orderby r.Wins descending 4select r; 5var racers2= from r in Formula1.GetChampions() 6orderby r.Country,r.LastName,r.FirstName 7select r; 8var racers3 = Formula1.GetChampions().OrderBy(r => r.Country).ThenBy(r => r.LastName).ThenBy(r => r.FirstName);
6、分组
1 2 var countries = from r in Formula1.GetChampions() 3 group r by r.Country into g 4orderby g.Count() descending, g.Key 5where g.Count() >= 2 6selectnew { Country = g.Key, Count = g.Count() }; 7 8var item = Formula1.GetChampions().GroupBy(r => r.Country).OrderByDescending(p => p.Count()). 9 ThenBy(p => p.Key).Where(p => p.Count() >= 2).Select(p => new { Country = p.Key, Count = p.Count() }); 10
7、内链接
1 var racers = from r in Formula1.GetChampions() 2from y in r.Years 3selectnew 4 { 5 Year = y, 6 Name = r.FirstName + "" + r.LastName 7 }; 8 9var teams = from t in Formula1.GetContructorChampions() 10from y in t.Years 11selectnew12 { 13 Year = y, 14 Name = t.Name 15 };
8、左连接
1 var racersAndTeams = 2 (from r in racers 3 join t in teams on r.Year equals t.Year into rt 4from t in rt.DefaultIfEmpty() 5orderby r.Year 6selectnew 7 { 8 Year = r.Year, 9 Champion = r.Name, 10 Constructor = t == null ? "no constructor championship" : t.Name 11 }).Take(10);
9、zip 合并
1 var racerNames = from r in Formula1.GetChampions() 2where r.Country == "Italy" 3orderby r.Wins descending 4selectnew 5 { 6 Name = r.FirstName + "" + r.LastName 7 }; 8 9var racerNamesAndStarts = from r in Formula1.GetChampions() 10where r.Country == "Italy"11orderby r.Wins descending 12selectnew13 { 14 LastName = r.LastName, 15 Starts = r.Starts 16 }; 171819var racers = racerNames.Zip(racerNamesAndStarts, (first, second) => first.Name + ", starts: " + second.Starts);
10、聚合函数
1 var query = from r in Formula1.GetChampions() 2 let numberYears = r.Years.Count() 3 4where numberYears >= 3 5orderby numberYears descending, r.LastName 6selectnew 7 { 8 Name = r.FirstName + "" + r.LastName, 9 TimesChampion = numberYears 10 }; 1112var countries = (from c in13from r in Formula1.GetChampions() 14 group r by r.Country into c 15selectnew16 { 17 Country = c.Key, 18 Wins = (from r1 in c 19select r1.Wins).Sum() 20 } 21orderby c.Wins descending, c.Country 22select c).Take(5);
原文:http://www.cnblogs.com/farmer-y/p/5977483.html
内容总结
以上是互联网集市为您收集整理的C# LINQ标准查询操作符全部内容,希望文章能够帮你解决C# LINQ标准查询操作符所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。