LINQ
2022-10-18 本文已影响0人
山猪打不过家猪
1.推断类型var关键字
var可以根据变量的初始值自动推断局部变量的类型
image.png
2.匿名类的 使用
class Program
{
static void Main(string[] args)
{
//创建一个学员对象
Student stu1 = new Student(1001, "fxx1");
//通过匿名类创建一个学院
var ojbStudent = new
{
Name = "fxx",
Age = 18,
ClassName = "WEB"
};
Console.WriteLine("姓名{0},年龄{1},班级{2}", ojbStudent.Name, ojbStudent.Age, ojbStudent.ClassName);
}
}
3.简单扩展方法的应用
image.pngimage.png
ExtendMethod.cs
namespace ConsoleApp1
{
/// <summary>
/// 扩展方法必须是静态类
/// </summary>
static class ExtendMethod
{
public static int GetAvg(this int num)
{
return num / 5;
}
public static string GetStudentInfo(this string stuInfo)
{
return $"我的学生名字是:{stuInfo}";
}
}
}
Program.cs
static void Main(string[] args)
{
string stuName = "fxx";
Console.WriteLine(stuName.GetStudentInfo());
}
- 为密封类Student添加扩展方法
static void Main(string[] args)
{
Student fxx = new Student();
fxx.StuName = "fxx";
Console.WriteLine(fxx.ShowStuInfo());
}
Student.cs
namespace ConsoleApp1
{
sealed class Student
{
public Student() { }
public Student(int id,string stuNam)
{
this.Id = id;
this.StuName = stuNam;
}
public int Id { get; set; }
public string StuName { get; set; }
}
}
4. 委托的基本使用
image.pngnamespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//【3】创建委托对象
CalculatorDelegate objCal = new CalculatorDelegate(Add);
//【4】通过委托调用方法, 而不是使用方法直接调用
int result = objCal(10, 20);
Console.WriteLine(result);
//【5】断开当前委托关联的方法
objCal -= Add;
//【6】重新委托
objCal += Sub;
int result2 = objCal(10, 5);
Console.WriteLine(result2);
}
//【2】根据委托定义具体方法
static int Add(int a, int b)
{
return a + b;
}
static int Sub(int a, int b)
{
return a - b;
}
}
//【1】声明委托(定义一个函数的原型:返回值+参数类型的个数)
public delegate int CalculatorDelegate(int a, int b);
}
5.匿名方法和匿名函数Lambda
image.png- 匿名方法
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
CalculatorDelegate objCal = delegate (int a, int b)
{
return a + b;
};
int result = objCal(10, 20);
Console.WriteLine(result);
}
}
//【1】声明委托(定义一个函数的原型:返回值+参数类型的个数)
public delegate int CalculatorDelegate(int a, int b);
}
-
Lamba表达式
image.png
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
CalculatorDelegate objCal = (a, b) => { return a + b; };
int result = objCal(10, 20);
Console.WriteLine(result);
}
}
//【1】声明委托(定义一个函数的原型:返回值+参数类型的个数)
public delegate int CalculatorDelegate(int a, int b);
}
- 如果定义一个参数,则可以简写
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
CalculatorDelegate objCal = a => a * a;
int result = objCal(10);
Console.WriteLine(result);
}
}
//【1】声明委托(定义一个函数的原型:返回值+参数类型的个数)
public delegate int CalculatorDelegate(int a);
}
6.LINQ查询语句和方法
image.png- 使用LINQ
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,13,20};
//使用LINQ
var list = from num in nums
where num % 2 != 0
orderby num descending
select num;
foreach (int item in list)
{
Console.WriteLine(item);
}
}
}
}
7.LINQ查询方法
7.1 select()
image.pngnamespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,13,20 };
var list = nums.Select(item => item * 2);
foreach (int item in list)
{
Console.WriteLine(item);
}
}
}
}
7.2 where()
image.pngnamespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 40, 20, 4, 13};
var list = nums.Where(item => item % 2==0).Select(i=>i*i).OrderBy(item=>item);
foreach (int item in list)
{
Console.WriteLine(item);
}
}
}
}
>>>
16
400
1600
- 例2
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string[] nums = { "fxx", "艾热","王以太","PGone","aa","bb"};
var list = nums.Where(item => item.Length == 2).Select(item => item).OrderByDescending(item=>item.Substring(0,1));
foreach (string item in list)
{
Console.WriteLine(item);
}
}
}
}
>>>
艾热
bb
aa
7.3 GroupBy()
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string[] nums = { "fxx", "艾热","王以太","PGone","bs","bb","艾滋"};
var list = nums.Where(item => item.Length == 2).Select(item => item).GroupBy(item=>item.Substring(0,1));
foreach (var item in list)
{
Console.WriteLine("***********");
Console.WriteLine($"分组的字段是:{item.Key}");
foreach (var i in item)
{
Console.WriteLine(i);
}
}
}
}
}
>>>
***********
分组的字段是:艾
艾热
艾滋
***********
分组的字段是:b
bs
bb
8.LINQ的查询时机
image.png- Count()立即执行
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 2, 3, 4, 5, 6, 7 };
var list = nums.Where(item => item % 2 == 0).Select(item => item).Count();
Console.WriteLine(list.ToString());
}
}
}
>>>
3
9.LINQ 的两种查询形式
image.png10. LINQ 子句
image.pngnamespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student() { StuName = "fxx", ScoreList = new List<int>() { 80, 91, 44 } };
Student stu2 = new Student() { StuName = "axx", ScoreList = new List<int>() { 80, 50, 44 } };
Student stu3 = new Student() { StuName = "bxx", ScoreList = new List<int>() { 80, 55, 98 } };
//封装到集合
List<Student> StuList = new List<Student> { stu1, stu2, stu3 };
//查询成绩中有95分以上的学员
var result = from stu in StuList
from score in stu.ScoreList
where score > 90
select stu;
foreach (var item in result)
{
Console.WriteLine($"有分数大于90的学员姓名是:{item.StuName}");
}
}
}
}
-
两个数据源的联合查询
image.png
image.png
11.高级查询方法
image.pngimage.png
image.png
image.png
image.png
image.png