可以使用相同的基本编码模式来查询和转换 XML 文档、SQL 数据库、ADO.NET 数据集、.NET 集合中的数据以及对其有 LINQ 提供程序可用的任何其他格式的数据。
查询操作的三个部分
所有 LINQ 查询操作都由以下三个不同的操作组成:
-
获取数据源。
-
创建查询。
-
执行查询。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Linq 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //创建int类型的数组 14 //1.数据源 15 int[] myInt = new int[7] {0,1,2,3,4,5,6 }; 16 17 //2.创建查询 18 var myQuery = from s in myInt 19 where (s % 2) == 0 20 select s; 21 //3.执行查询 22 foreach (var item in myQuery) 23 { 24 Console.WriteLine("{0}",item); 25 } 26 27 Console.ReadKey(); 28 29 30 } 31 } 32 }