首先添加数据集合

 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     }

View Code

 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     }

View Code

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 var 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
 4                             orderby g.Count() descending, g.Key
 5                             where g.Count() >= 2
 6                             select new { Country = g.Key, Count = g.Count() };
 7
 8 var 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()
 2                          from y in r.Years
 3                          select new
 4                          {
 5                              Year = y,
 6                              Name = r.FirstName + " " + r.LastName
 7                          };
 8
 9             var teams = from t in Formula1.GetContructorChampions()
10                         from y in t.Years
11                         select new
12                         {
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
 4                from t in rt.DefaultIfEmpty()
 5                orderby r.Year
 6                select new
 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()
 2                              where r.Country == "Italy"
 3                              orderby r.Wins descending
 4                              select new
 5                              {
 6                                  Name = r.FirstName + " " + r.LastName
 7                              };
 8
 9             var racerNamesAndStarts = from r in Formula1.GetChampions()
10                                       where r.Country == "Italy"
11                                       orderby r.Wins descending
12                                       select new
13                                       {
14                                           LastName = r.LastName,
15                                           Starts = r.Starts
16                                       };
17
18
19             var 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
 4                         where numberYears >= 3
 5                         orderby numberYears descending, r.LastName
 6                         select new
 7                         {
 8                             Name = r.FirstName + " " + r.LastName,
 9                             TimesChampion = numberYears
10                         };
11
12  var countries = (from c in
13                                  from r in Formula1.GetChampions()
14                                  group r by r.Country into c
15                                  select new
16                                  {
17                                      Country = c.Key,
18                                      Wins = (from r1 in c
19                                              select r1.Wins).Sum()
20                                  }
21                              orderby c.Wins descending, c.Country
22                              select c).Take(5);

转载于:https://www.cnblogs.com/farmer-y/p/5977483.html

C# LINQ标准查询操作符相关推荐

  1. LINQ标准查询操作符

    本文基于.NET Framework 2.0编写 本文PDF下载 推荐大家下载本文的PDF进行阅读,可以方便的使用书签来阅读各个方法,而且代码中的关键字是高亮显示的. 转载请注明出处. 一.投影操作符 ...

  2. LINQ标准查询操作符详解

     一. 关于LINQ        LINQ 英文全称是"Language-Integrated Query",中文为"语言集成查询",它是微软首席架构师.De ...

  3. LINQ 的标准查询操作符

    摘自msdn (http://msdn.microsoft.com/zh-cn/magazine/cc337893.aspx) LINQ 的标准查询操作符 John Papa 代码下载位置: Data ...

  4. mysql linq any查询_LINQ标准查询操作符详解(转)

    一. 关于LINQ LINQ 英文全称是"Language-Integrated Query",中文为"语言集成查询",它是微软首席架构师.Delphi 之父和 ...

  5. 《C#本质论》读书笔记(14)支持标准查询操作符的集合接口

    14.2.集合初始化器 使用集合初始化器,程序员可以采用和数组相似的方式,在集合的实例化期间用一套初始的成员来构造这个集合.如果没有集合初始化器,就只有在集合实例化后才能显示添加到集合中--例如使用 ...

  6. NHibernate3剖析:Query篇之NHibernate.Linq标准查询

    本节内容 系列引入 NHibernate.Linq概述 标准查询运算符 结语 系列引入 NHibernate3.0剖析系列分别从Configuration篇.Mapping篇.Query篇.Sessi ...

  7. 一起谈.NET技术,NHibernate3.0剖析:Query篇之NHibernate.Linq标准查询

    系列引入 NHibernate3.0剖析系列分别从Configuration篇.Mapping篇.Query篇.Session策略篇.应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种 ...

  8. NHibernate3.0剖析:Query篇之NHibernate.Linq标准查询

    系列引入 NHibernate3.0剖析系列分别从Configuration篇.Mapping篇.Query篇.Session策略篇.应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种 ...

  9. LINQ:进阶 - LINQ 标准查询操作概述

    "标准查询运算符"是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> 接口或 I ...

最新文章

  1. curl命令具体解释
  2. 央视在世界杯高清直播中占了C位 它是怎么做到的?
  3. GARFIELD@01-19-2005
  4. 聊聊困扰很多同学的一个问题:是否要转方向 ?
  5. Openstack api security testing tools
  6. 求书:推荐阅读倡议书
  7. 简单比较init-method,afterPropertiesSet和BeanPostProcessor
  8. R语言中ggplot Theme Assist安装使用教程
  9. python牛顿法寻找极值_使用Python实现牛顿法求极值
  10. java条形码解析_Java 生成、识别条形码
  11. c51单片机流水灯程序汇编语言,基于51单片机的流水灯程序
  12. 数字逻辑实验一 门电路逻辑功能及测试
  13. 世界上第一个科学家是谁
  14. Web全栈架构师(三)——NodeJS+持久化学习笔记(2)
  15. BZOJ 3270: 博物馆 1778: 驱逐猪猡 【概率DP+高斯消元】
  16. 田忌赛马(贪心算法)C++
  17. 上海的大学计算机专业高考分数线公布,上海全部64所大学排名及分数线分析,想去魔都上学考生家长必看...
  18. 没有滴滴的顺风车,想有顺风车的滴滴
  19. centos7查看进程ps_Linux ps命令:查看所有进程信息
  20. GreenPlum 触发执行vacuum

热门文章

  1. 数学分析第二型曲线积分2021.6.2
  2. 【Git-2022总结】分布式代码版本控制工具【GitHub/Gitee/GitLab】
  3. 虚拟机服务器 资料安全,绝密:三步教你轻松窃取VMware虚拟机及其数据漏洞预警 -电脑资料...
  4. 纹理过滤函数glTexParameteri
  5. 基于python的情感分析案例-基于情感词典的python情感分析
  6. 王道书 P41 T19(循环单链表实现)
  7. 基于FPGA的DVB-S2、DVB-S2X标准的 LDPC 编码IP、译码 IP core
  8. 微信企业付款 ”错误码:NO_AUTH,错误信息:此IP地址不允许调用接口,如有需要请登录微信支付商户平台更改配置“报错的处理办法
  9. Windows 标准控件 ComboBox 的改造
  10. 系统集成资质取消后 ,偷偷崛起的ICSCE资质(信息化能力和信用评价资质)