LINQ(Language Integrated Query,语言集成查询),在C#语言中集成了查询语法,可以用相同的语法访问不同的数据源。
  LINQ提供了不同数据源的抽象层,所以可以使用相同的语法。
  这里主要介绍LINQ的核心原理和C#中支持C# LINQ查询的语言扩展。

1.语法
  使用LINQ查询出来自巴西的所以世界冠军。这里可以使用List<T>类的FindAll()方法,但使用LINQ查询语法更简单  

  1.     static void LINQQuery()
  2. {
  3. //
  4. var query = from r in Formula1.GetChampions()
  5. where r.Country == "Brazil"
  6. orderby r.Wins descending
  7. select r;
  8.  
  9. foreach (var r in query)
  10. {
  11. Console.WriteLine("{0:A}", r);
  12. }
  13.  
  14. }

  变量query只指定了LINQ查询。该查询不是通过这个赋值语句执行的,而是使用foreach循环访问查询时执行的。

2.扩展方法
  编译器会转换LINQ查询,以调用方法而不是LINQ查询。LINQ为IEnumerable<T>接口提供了各种扩展方法(扩展方法在http://www.cnblogs.com/afei-24/p/6703843.html介绍到),以便用户在实现了该接口的任意集合上使用LINQ查询。
  定义LINQ扩展方法的一个类是System.Linq名称空间中的Enumerable。只需要导入这个名称空间,就打开了这个类的扩展方法的作用域。下面是Where()扩展方法的实现代码:

  1.     public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,Func<TSource,bool> predicate)
  2. {
  3. foreach(TSource item in source)
  4. {
  5. if(predicate(item))
  6. {
  7. yield return item;
  8. }
  9.  
  10. }
  11. }

  因为Where()作为一个泛型方法,所以它可以用于包含在集合中的任意类型。实现了IEnumerable<T>接口的任意集合都支持它。

3.推迟查询的执行
  前面提到,在运行期间定义LINQ查询表达式时,查询不会运行。查询在迭代数据项时才会运行。
  举个例子:  

  1.     static void DeferredQuery()
  2. {
  3. var names = new List<string> { "Nino", "Alberto", "Juan", "Mike", "Phil" };
  4.  
  5. var namesWithJ = from n in names
  6. where n.StartsWith("J")
  7. orderby n
  8. select n;
  9.  
  10. Console.WriteLine("First iteration");
  11. foreach (string name in namesWithJ)
  12. {
  13. Console.WriteLine(name);
  14. }
  15. Console.WriteLine();
  16.  
  17. names.Add("John");
  18. names.Add("Jim");
  19. names.Add("Jack");
  20. names.Add("Denny");
  21.  
  22. Console.WriteLine("Second iteration");
  23. foreach (string name in namesWithJ)
  24. {
  25. Console.WriteLine(name);
  26. }
  27.  
  28. }

  输出:
  
  因为查询在迭代时才执行,所以在第一次输出后有添加项再输出,会显示又添加的项。

  但在调用方法ToArray(),ToList等方法时,不会延迟执行:  

  1.     static void NotDeferredQuery()
  2. {
  3. var names = new List<string> { "Nino", "Alberto", "Juan", "Mike", "Phil" };
  4.  
  5. var namesWithJ = (from n in names
  6. where n.StartsWith("J")
  7. orderby n
  8. select n).ToList();
  9.  
  10. Console.WriteLine("First iteration");
  11. foreach (string name in namesWithJ)
  12. {
  13. Console.WriteLine(name);
  14. }
  15. Console.WriteLine();
  16.  
  17. names.Add("John");
  18. names.Add("Jim");
  19. names.Add("Jack");
  20. names.Add("Denny");
  21.  
  22. Console.WriteLine("Second iteration");
  23. foreach (string name in namesWithJ)
  24. {
  25. Console.WriteLine(name);
  26. }
  27.  
  28. }

  输出:
  

下面是用到的类,后续的也需要用到这些代码。

  1. //这个类创建需要的列表
  2. public static class Formula1
  3. {
  4. private static List<Racer> racers;
  5. //返回一组赛车手
  6. public static IList<Racer> GetChampions()
  7. {
  8. if (racers == null)
  9. {
  10. racers = new List<Racer>();
  11. racers.Add(new Racer("Nino", "Farina", "Italy", , ,
               new int[] { }, new string[] { "Alfa Romeo" }));
  12. racers.Add(new Racer("Alberto", "Ascari", "Italy", , ,
              new int[] { , }, new string[] { "Ferrari" }));
  13. racers.Add(new Racer("Juan Manuel", "Fangio", "Argentina", , ,
               new int[] { , , , , }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" }));
  14. racers.Add(new Racer("Mike", "Hawthorn", "UK", , ,
               new int[] { }, new string[] { "Ferrari" }));
  15. racers.Add(new Racer("Phil", "Hill", "USA", , ,
               new int[] { }, new string[] { "Ferrari" }));
  16. racers.Add(new Racer("John", "Surtees", "UK", , ,
               new int[] { }, new string[] { "Ferrari" }));
  17. racers.Add(new Racer("Jim", "Clark", "UK", , ,
              new int[] { , }, new string[] { "Lotus" }));
  18. racers.Add(new Racer("Jack", "Brabham", "Australia", , ,
               new int[] { , , }, new string[] { "Cooper", "Brabham" }));
  19. racers.Add(new Racer("Denny", "Hulme", "New Zealand", , ,
              new int[] { }, new string[] { "Brabham" }));
  20. }
  21.  
  22. return racers;
  23. }
  24.  
  25. private static List<Team> teams;
  26. //返回一组冠军车队
  27. public static IList<Team> GetContructorChampions()
  28. {
  29. if (teams == null)
  30. {
  31. teams = new List<Team>()
  32. {
  33. new Team("Vanwall", ),
  34. new Team("Cooper", , ),
  35. new Team("Ferrari", , , , , , , , , ,
                        2000, , , , , , ),
  36. new Team("BRM", ),
  37. new Team("Lotus", , , , , , , ),
  38. new Team("Brabham", , ),
  39. new Team("Matra", ),
  40. new Team("Tyrrell", ),
  41. new Team("McLaren", , , , , , , , ),
  42. new Team("Williams", , , , , , , , , ),
  43. new Team("Benetton", ),
  44. new Team("Renault", , ),
  45. new Team("Brawn GP", ),
  46. new Team("Red Bull Racing", , )
  47. };
  48. }
  49. return teams;
  50. }
  51.  
  52. private static List<Championship> championships;
  53. //返回GetChampionships类型的集合
  54. public static IEnumerable<Championship> GetChampionships()
  55. {
  56. if (championships == null)
  57. {
  58. championships = new List<Championship>();
  59. championships.Add(new Championship
  60. {
  61. Year = ,
  62. First = "Nino Farina",
  63. Second = "Juan Manuel Fangio",
  64. Third = "Luigi Fagioli"
  65. });
  66. championships.Add(new Championship
  67. {
  68. Year = ,
  69. First = "Juan Manuel Fangio",
  70. Second = "Alberto Ascari",
  71. Third = "Froilan Gonzalez"
  72. });
  73. championships.Add(new Championship
  74. {
  75. Year = ,
  76. First = "Alberto Ascari",
  77. Second = "Nino Farina",
  78. Third = "Piero Taruffi"
  79. });
  80. championships.Add(new Championship
  81. {
  82. Year = ,
  83. First = "Alberto Ascari",
  84. Second = "Juan Manuel Fangio",
  85. Third = "Nino Farina"
  86. });
  87. championships.Add(new Championship
  88. {
  89. Year = ,
  90. First = "Juan Manuel Fangio",
  91. Second = "Froilan Gonzalez",
  92. Third = "Mike Hawthorn"
  93. });
  94. championships.Add(new Championship
  95. {
  96. Year = ,
  97. First = "Juan Manuel Fangio",
  98. Second = "Stirling Moss",
  99. Third = "Eugenio Castellotti"
  100. });
  101. championships.Add(new Championship
  102. {
  103. Year = ,
  104. First = "Juan Manuel Fangio",
  105. Second = "Stirling Moss",
  106. Third = "Peter Collins"
  107. });
  108.  
  109. }
  110. return championships;
  111. }
  112. }
  1. //车手类
  2.  
  3. [Serializable]
  4. public class Racer : IComparable<Racer>, IFormattable
  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. //单值属性
  21. public string FirstName { get; set; }
  22. public string LastName { get; set; }
  23. public string Country { get; set; }
  24. public int Wins { get; set; }
  25. public int Starts { get; set; }
  26. //多值属性,车手可能多次获得冠军,所在的车队也可能不同
  27. public IEnumerable<string> Cars { get; private set; }
  28. public IEnumerable<int> Years { get; private set; }
  29.  
  30. public override string ToString()
  31. {
  32. return String.Format("{0} {1}", FirstName, LastName);
  33. }
  34.  
  35. public int CompareTo(Racer other)
  36. {
  37. if (other == null) return -;
  38. return string.Compare(this.LastName, other.LastName);
  39. }
  40.  
  41. public string ToString(string format)
  42. {
  43. return ToString(format, null);
  44. }
  45.  
  46. public string ToString(string format,
  47. IFormatProvider formatProvider)
  48. {
  49. switch (format)
  50. {
  51. case null:
  52. case "N":
  53. return ToString();
  54. case "F":
  55. return FirstName;
  56. case "L":
  57. return LastName;
  58. case "C":
  59. return Country;
  60. case "S":
  61. return Starts.ToString();
  62. case "W":
  63. return Wins.ToString();
  64. case "A":
  65. return String.Format("{0} {1}, {2}; starts: {3}, wins: {4}",
  66. FirstName, LastName, Country, Starts, Wins);
  67. default:
  68. throw new FormatException(String.Format("Format {0} not supported", format));
  69. }
  70. }
  71. }
  1. //获得冠军的车队
  2. [Serializable]
  3. public class Team
  4. {
  5. public Team(string name, params int[] years)
  6. {
  7. this.Name = name;
  8. this.Years = new List<int>(years);
  9. }
  10. public string Name { get; private set; }
  11. public IEnumerable<int> Years { get; private set; }
  12. }
  13.  
  14. //获奖选手和年份
  15. public class Championship
  16. {
  17. public int Year { get; set; }
  18. public string First { get; set; }
  19. public string Second { get; set; }
  20. public string Third { get; set; }
  21. }

LINQ基础(一)的更多相关文章

  1. [.net 面向对象编程基础] (19) LINQ基础

    [.net 面向对象编程基础] (19)  LINQ基础 上两节我们介绍了.net的数组.集合和泛型.我们说到,数组是从以前编程语言延伸过来的一种引用类型,采用事先定义长度分配存储区域的方式.而集合是 ...

  2. LINQ基础概述

    介绍LINQ基础之前,首说一下LINQ 的历史和LINQ是什么,然后说一下学习 LINQ要了解的东西和 LINQ基础语法   LINQ 的历史 从语言方面的进化 –委托 –匿名方法 –Lambda表达 ...

  3. LINQ基础(二)

    本文主要介绍LINQ查询操作符 LINQ查询为最常用的操作符定义了一个声明语法.还有许多查询操作符可用于Enumerable类. 下面的例子需要用到LINQ基础(一)(http://www.cnblo ...

  4. LINQ基础(三)

    一.并行LINQ System.Linq名称空间中包含的类ParallelEnumerable可以分解查询的工作,使其分布在多个线程上. 尽管Enumerable类给IEnumerable<T& ...

  5. Linq基础操作之Select,Where,OrderBy,ThenBy源码分析

    Linq基础操作之Select,Where,OrderBy,ThenBy源码分析 二:Select 它是延迟执行.yield有得一拼,因为他们都是生成了一个枚举类. if (source is TSo ...

  6. Linq基础必备

    1.linq基础必备之对象初始化器和匿名类型因果分析   3. 一:对象初始化器 1.就是在new的时候给公共属性赋值的一种方式 2. 在没有初始化器之前的时候,我们是怎么初始化的呢??? 1. 构造 ...

  7. 20.C#LINQ基础和简单使用(十一章11.1-11.2)

    终于看到了第11章,之前虽然也有看过,但没有太仔细,在工作中也偶尔会使用,但不明白其中的原理,那现在就来讲讲LINQ,做一做书虫~~ 首先先了解下LINQ的三个要点: LINQ不能把非常复杂的查询表达 ...

  8. LINQ之路(1):LINQ基础

    本文将从什么是LINQ(What).为什么使用LINQ(Why)以及如何使用LINQ(How)三个方面来进行说明. 1.什么是LINQ LINQ(Language Integrated Query)是 ...

  9. Linq基础知识小记四之操作EF

    1.EF简介 EF之于Linq,EF是一种包含Linq功能对象关系映射技术.EF对数据库架构和我们查询的类型进行更好的解耦,使用EF,我们查询的对象不再是C#类,而是更高层的抽象:Entity Dat ...

  10. C#3.0新增功能09 LINQ 基础01 语言集成查询

    连载目录    [已更新最新开发文章,点击查看详细] 语言集成查询 (LINQ) 是一系列直接将查询功能集成到 C# 语言的技术统称. 数据查询历来都表示为简单的字符串,没有编译时类型检查或 Inte ...

随机推荐

  1. MySQL从库忽略某些错误

    z熬配置MySQL主从同步的时候常常会因为主库的中SQL语句的错误造成从库的同步出现错误,一旦从库同步出现错误就会造成同步的卡壳影响后续的同步: 可以在从库的配置文件中加入如下的参数,使从库可以自动忽 ...

  2. pyqt样式表语法笔记(中)

    pyqt样式表语法笔记(中) pyqt QSS python 样式表 一.弹窗 在日常的各种桌面软件的使用中,我们都会碰到弹窗.例如注册,登录的时候,会有相应的信息弹窗,这里就以信息收集弹窗为例进行弹 ...

  3. 浅谈MVC缓存

    缓存是将信息放在内存中以避免频繁访问数据库从数据库中提取数据,在系统优化过程中,缓存是比较普遍的优化做法和见效比较快的做法. 对于MVC有Control缓存和Action缓存. 一.Control缓存 ...

  4. Servlet实现的三种方式

    实现Servlet的三种方式:一个实现,两个继承 /*========================================== * servlet的执行过程: * 1.创建servlet对 ...

  5. response.getWriter().write()与out.print()的区别(转)

    1.首先介绍write()和print()方法的区别:  (1).write():仅支持输出字符类型数据,字符.字符数组.字符串等  (2).print():可以将各种类型(包括Object)的数据通 ...

  6. webix .datatable 表格分页

    grid表格返回参数大都是 以下这种格式(参数名可能不一样) { data:[{...},{...} ...], count:39 } webix的参数格式为 { data:[{...},{...}, ...

  7. 【Electron】Electron开发入门(三):main process和web page 通信

    一.main process 和 web page 通信 electron框架主进程(Main Process)与嵌入的网页(web page,也就是renderer process)之间的通信 Ma ...

  8. Python之路-awk文本处理

    作业一:整理博客,内容包含awk.变量.运算符.if多分支 一.awk 1.awk是一个优秀的文本处理工具,多用来处理含有特殊分隔符的内容 常见用法 awk -F:  {print $1,$4} 作业 ...

  9. 《连载 | 物联网框架ServerSuperIO教程》- 18.集成OPC Client,及使用步骤

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

  10. java 基础知识九 类与对象

    java  基础知识九  类与对象 1.OO(Object–Oriented )面向对象,OO方法(Object-Oriented Method,面向对象方法,面向对象的方法)是一种把面向对象的思想应 ...