LINQ(Language Integrated Query)
LINQ是.net框架的扩展,它允许我们以使用SQL查询数据库的方式来查询数据集合。
使用LINQ,你可以从数据库,程序对象的集合以及XML文档中查询数据。
查询语句
1 using System; 2 using System.Linq; 3 4 namespace Test 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 int[] numbers = { 2, 5, 28, 31, 17, 17, 42 }; 11 12 var numsQuery = from n in numbers 13 where n < 20 14 select n;//查询语句 15 16 var numsMethod = numbers.Where(x => x < 20);//方法语句 17 18 int numsCount = (from n in numbers 19 where n < 20 20 select n).Count();//两种结合 21 22 foreach (var x in numsQuery) 23 { 24 Console.Write("{0} ", x); 25 } 26 Console.WriteLine(); 27 28 foreach (var x in numsMethod) 29 { 30 Console.Write("{0} ", x); 31 } 32 Console.WriteLine(); 33 34 Console.WriteLine(numsCount); 35 } 36 } 37 }
from 迭代变量 in 要查询的集合名字
join 指定另外的集合和ID引用它 on 第一个集合的项 equals 第二个集合的项
from...let...where:
static void Main() { var groupA = new[] {3, 4, 5, 6}; var groupB = new[] {6, 7, 8, 9}; var someInts = from a in groupA from b in groupB let sum = a + b where sum == 12 select new {a, b, sum}; foreach (var a in someInts) Console.WriteLine(a); }
from...orderby...select
static void Main() { var group = new[] {8, 6, 3, 9, 2}; var result = from }
待补充。。