21.1 什么是LINQ
21.2 LINQ提供程序
21.3 查询语法和方法语法
21.4 查询变量
21.5 查询表达式的结构
21.5.1 from 子句
21.5.2 join 子句
21.5.3 什么是联结
21.5.4 查询主体中的 from...let...where 片段
21.5.5 orderby 子句
1 static void Main(string[] args) 2 { 3 int[] numbers = { 2, 12, 5, 15 }; 4 IEnumerable<int> lowNums = from n in numbers 5 where n < 10 6 select n; 7 foreach (var lowNum in lowNums) 8 { 9 Console.Write("{0}, ",lowNum); 10 } 11 }
结果:2,5,
匿名类型
创建对象表达式时初始化新类实例的字段和属性,这种形式的创建表达式由三部分组成:new关键字、类名或构造函数以及对象初始化器。对象初始化器在一组大括号内包含了逗号分隔的成员初始化列表。
创建匿名类型的变量使用相同形式,但没有类名和构造方法。
匿名类型实例:
static void Main(string[] args) { var student = new { LName = "Jones", FName = "Mary", Age = 19, Major = "History" }; Console.WriteLine( "{0}{1},Age {2},Major:{3}", student.FName, student.LName, student.Age, student.Major ); }
查询语法:是声明形式,像SQL。
方法语法:是命令形式,使用标准方法调用,方法是一组叫做标准查询运算符的方法。
static void Main(string[] args) { int[] numbers = { 2, 5, 28, 31, 17, 16, 42 }; var numsQuery = from n in numbers //查询语法 where n < 20 select n; var numsMethod = numbers.Where(x => x < 20); //方法语法 int numsCount = (from n in numbers //两种形式组合 where n < 20 select n).Count(); foreach (var i in numsQuery) { Console.Write("{0},", i); } Console.WriteLine(); foreach (var i in numsMethod) { Console.Write("{0},", i); } Console.WriteLine(); Console.WriteLine(numsCount); }
LINQ返回两种类型的结果——
一个枚举(可枚举的一组数据,不是枚举类型),它列出了满足查询参数的的项列表;
一个叫做标量(scalar)的单一值(某种摘要形式)。
1 static void Main(string[] args) 2 { 3 int[] arr1 = { 2, 5, 28 }; 4 IEnumerable<int> lowNums = from i in arr1 //返回枚举数 5 where i < 20 6 select i; 7 var numsCount = (from n in arr1 8 where n < 20 9 select n).Count(); //返回一个整数 10 foreach (var lowNum in lowNums) 11 { 12 Console.Write(lowNum); 13 } 14 Console.WriteLine(); 15 Console.WriteLine(numsCount); 16 //结果: 17 //25 18 //2 19 }
from Type Item in Item
join Identifier inColiiection2 on Field1 equals Filed2
示例:
1 class Program 2 { 3 public class Student 4 { 5 // Declare classes. 6 public int StID; 7 public string LastName; 8 } 9 10 public class CourseStudent 11 { 12 public string CourseName; 13 public int StID; 14 } 15 16 // Initialize arrays. 初始化数组 17 private static CourseStudent[] studentsInCourses = new CourseStudent[] 18 { 19 new CourseStudent {CourseName = "Art", StID = 1}, 20 new CourseStudent {CourseName = "Art", StID = 2}, 21 new CourseStudent {CourseName = "History", StID = 1}, 22 new CourseStudent {CourseName = "History", StID = 3}, 23 new CourseStudent {CourseName = "Physics", StID = 3}, 24 }; 25 26 private static Student[] students = new Student[] 27 { 28 new Student {StID = 1, LastName = "Carson"}, 29 new Student {StID = 2, LastName = "Klassen"}, 30 new Student {StID = 3, LastName = "Fleming"}, 31 }; 32 33 private static void Main() 34 { 35 // Find the last names of the students taking history. 36 var query = from s in students 37 join c in studentsInCourses on s.StID equals c.StID 38 where c.CourseName == "History" 39 select s.LastName; 40 // Display the names of the students taking history. 41 foreach (var q in query) 42 Console.WriteLine("Student taking History: {0}", q); 43 } 44 }
结果:
Student taking History: Carson
Student taking History: Fleming
请按任意键继续. . .
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 where a > 4 && b <= 8 select new { a, b, sum = a + b }; foreach (var a in someInts) Console.WriteLine(a); }
let子句接受一个表达式的运算并且把它赋值给一个需要在其他运算中使用的标识符。
let Identifier(标识符) = Expression(表达式)
1 static void Main() 2 { 3 var groupA = new[] { 3, 4, 5, 6 }; 4 var groupB = new[] { 6, 7, 8, 9 }; 5 var someInts = from a in groupA 6 from b in groupB 7 let sum = a + b //新的变量中保存结果 8 where sum == 12 9 select new { a, b, sum }; 10 foreach (var a in someInts) 11 Console.WriteLine(a); 12 }
where BooleamExpression
1.只要在from..let..where部分始终,表达式可以存在多个where子句。
2.一个项目必须满足 where 子句才能避免在之后被过滤。
static void Main() { var groupA = new[] { 3, 4, 5, 6 }; var groupB = new[] { 6, 7, 8, 9 }; var someInts = from int a in groupA from int b in groupB let sum = a + b where sum >= 11 where a == 4 select new { a, b, sum }; foreach (var a in someInts) Console.WriteLine(a); }
1.orderby默认为升序。我们可以设置ascending和descending选择升序或降序。
2.可以有任意多个子句,他们必须使用逗号分隔。
1 static void Main() 2 { 3 var students = new[] // Array of objects of an anonymous type 4 { 5 new {LName = "Jones", FName = "Mary", Age = 19, Major = "History"}, 6 new {LName = "Smith", FName = "Bob", Age = 20, Major = "CompSci"}, 7 new {LName = "Fleming", FName = "Carol", Age = 21, Major = "History"} 8 }; 9 var query = from student in students 10 orderby student.Age descending 11 select student; 12 foreach (var s in query) 13 { 14 Console.WriteLine("{0}, {1}: {2} - {3}", 15 s.LName, s.FName, s.Age, s.Major); 16 } 17 }
Fleming, Carol: 21 - History
Bob: 20 - CompSci
Jones, Mary: 19 - History
请按任意键继续. . .