Linq学习

参考书籍《Illustrated C#7, 5th Edition》

什么是LINQ?

  1. LINQ = Language Integrated Query 发音"link"
  2. LINQ是.NET框架的扩展。用类似SQL语法查询数据库一样,来查询数据集。SQL查询数据库,database。LINQ查询数据集,data collection

    原因是SQL查询规范的表格,也即数据库,而LINQ不一定是规范的数据。SQL查询的是数据库,而LINQ查询可以查程序数据集。

  3. 第二点补充,使用LINQ可以查询database(数据库),对象集合(object collection),XML文档(XML document)等

第一个例子:

  1. using
    							System;
    							
  2. using
    							System.Collections.Generic; //集合 
  3. using
    							System.Linq; 
  4.  
  5. class
    							Program
    						
  6. 
    						{
    					
  7. 
    						static
    								void Main() 
  8. 
    						{
    					
  9. 
    						int[] numbers = { 2, 12, 5, 15 }; // Data source 
  10. 
    						IEnumerable<int> lowNums = // Define and store the query. 
  11. 
    						from n in numbers
    								
  12. 
    						where n < 10 
  13. 
    						select n;
    							
  14.  
  15. 
    						foreach
    								(var x in lowNums) // Execute the query. 
  16. 
    						Console.Write($"{ x }, "); 
  17. 
    						}
    					
  18. }
    					

  

猜你喜欢

转载自www.cnblogs.com/ifconfig/p/13184362.html