关键词搜索

源码搜索 ×
×

C#基础学习--LINQ

发布2023-04-20浏览808次

详情内容

目录

什么是LINQ

LINQ提供程序

匿名类型

 投影初始化语句

方法语法和查询语法

 查询变量

 查询表达式的结构

from子句

join子句

什么是联结

 查询主体中的 from...let...where片段

from 子句

let 子句

where  子句

orderby 子句

​编辑select....group子句

查询中的匿名类型

 group子句

​编辑查询延续 :into


什么是LINQ

从对象获取数据的方法一直都是作为程序的一部分而设计的,然而使用LINQ可以很轻松的查询对象集合

LINQ提供程序

匿名类型

匿名类型经常用于LINQ查询的结果之中

匿名类型的对象创建表达式:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Csharpzuoye
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. //必须使用var 匿名对象初始化语句
  12. var student = new { Name = "Mary Jones", Age = 19, Major = "History" };
  13. Console.WriteLine("{0},Age {1},Major:{2}", student.Name, student.Age, student.Major);
  14. }
  15. }
  16. }

 投影初始化语句

除了对象初始化语句的赋值形式,匿名类型的对象初始化语句还有两种允许的形式:简单标识符成员访问表达式

 如果编译器遇到了另一个具有相同的参数名,相同的推断类型和相同顺序的匿名类型,它会重用这个类型并直接创建新的实例,不会创建新的匿名类型

方法语法和查询语法

 示例:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Csharpzuoye
  7. {
  8. class Program
  9. {
  10. static void Main()
  11. {
  12. int[] numbers = { 2, 5, 28, 31, 17, 16, 42 };
  13. var numsQuery = from n in numbers //查询语法
  14. where n < 20
  15. select n;
  16. var numsMethod = numbers.Where(x => x < 20); //方法语法
  17. int numsCount = (from n in numbers //两种方法的组合
  18. where n < 20
  19. select n).Count();
  20. foreach (var x in numsQuery)
  21. Console.Write("{0} ", x);
  22. Console.WriteLine();
  23. foreach(var x in numsMethod)
  24. Console.Write("{0} ", x);
  25. Console.WriteLine();
  26. Console.WriteLine(numsCount);
  27. }
  28. }
  29. }

 查询变量

LINQ 查询可以返回两种类型的结果--可以是一个枚举(可枚举的一组数据,不是枚举类型)它满足查询参数的项列表,也可以是一个叫做标量的的单一值,它满足查询条件的结果的某种摘要形式

 查询表达式的结构

查询表达式有查询体后的from子句组成

from子句

from子句指定了数据源使用的数据集合,它还引入了迭代变量

join子句

联结的语法如下:

   使用联结来结合两个或更多集合中的数据

    联结操作接受两个集合然后创建一个临时的对象集合,每一个对象包含原始集合对象中的所有字段

什么是联结

LINQ 中的 join 接受两个集合然后创建一个新的集合,每一个元素包含两个原始集合中的原始成员

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Csharpzuoye
  7. {
  8. class Program
  9. {
  10. public class Student //声明类
  11. {
  12. public int StID;
  13. public string LastName;
  14. }
  15. public class CourseStudent
  16. {
  17. public string CourseName;
  18. public int StID;
  19. }
  20. static Student[] students = new Student[]
  21. {
  22. new Student{StID = 1,LastName = "Carson"},
  23. new Student{StID = 2,LastName = "Klassen"},
  24. new Student{StID = 3,LastName = "Fleming"},
  25. };
  26. //初始化数组
  27. static CourseStudent[] studentsInCourses = new CourseStudent[]
  28. {
  29. new CourseStudent {CourseName = "Art",StID = 1},
  30. new CourseStudent {CourseName = "Art",StID = 2},
  31. new CourseStudent {CourseName = "History",StID = 1},
  32. new CourseStudent {CourseName = "History",StID = 3},
  33. new CourseStudent {CourseName = "Physics",StID = 3},
  34. };
  35. static void Main()
  36. {
  37. //查找所有选择了历史课的学生的姓氏
  38. var query = from s in students
  39. join c in studentsInCourses on s.StID equals c.StID
  40. where c.CourseName == "History"
  41. select s.LastName;
  42. //显示所有选择了历史课的学生的名字
  43. foreach (var q in query)
  44. Console.WriteLine("Student taking History: {0}", q);
  45. }
  46. }
  47. }

 

 查询主体中的 from...let...where片段

from 子句

查询表达式从必需的from子句开始,后面跟的是查询主体。主体本身可以从任何数量的其他from子句开始,每一个from子句都指定了一个额外的源数

 

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Csharpzuoye
  7. {
  8. class Program
  9. {
  10. static void Main()
  11. {
  12. var groupA = new[] { 3, 4, 5, 6 };
  13. var groupB = new[] { 6, 7, 8, 9 };
  14. var someInts = from a in groupA //必需的第一个from
  15. from b in groupB //查询主体的第一个子句
  16. where a > 4 && b <= 8
  17. select new { a, b, sum = a + b }; //匿名类型对象
  18. foreach (var a in someInts)
  19. Console.WriteLine(a);
  20. }
  21. }
  22. }

 

据集合并引入了要在之后运算的迭代变量,所有from子句的语法和含义都是一样的

let 子句

let 子句接受一个表达式的运算并且把它赋值给一个需要在其他运算中使用的标识符。let 子句的语法如下:

let Identifier = Expression

where  子句

where 子句根据之后的运算来去除不符合指定条件的项,语法如下:

where BooleanExpression

只要是在from...let...where部分中,查询表达式可以有任意多个where子句

一个项必须满足where 子句才能避免在之后被过滤

orderby 子句

orderby 子句接受一个表达式并根据表达式按顺序返回结果项

select....group子句

查询中的匿名类型

查询结果可以由原始集合中的项,项的某些字段或匿名类型组成

 group子句

group 子句把 select 的对象根据一些标准进行分组

查询延续 :into

查询延续子句可以接受查询的一部分结果并赋予一个名字,从而可以在查询的另一部分中使用。

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载