为什么要学Linq?
当然为了让数据处理变得更简单,为了更加的人性化。
在了解Linq语句之前,我们通过委托到lambda来推出linq的基本语法
委托->Lambda->Linq
static void Main(string[] args)
{
/*med m = new med(sum);
int result = m(1, 2);
Console.WriteLine(result);*/
Func<int,int,int> f= sum;//Func带返回值得泛型类型;前两个int是参数的类型,后面一个是方法sum的返回值类型
Console.WriteLine(f(2, 3));
Action a = F;//Action不带返回值的泛型类型;
a();
//匿名函数
Func<int, int,int> s = delegate (int i, int n)
{
return i + n;
};
Console.WriteLine(s(5, 6));
//lambada表达式 返回值只有一行代码的(可以删掉return和大括号)参数只有一个的时候(可以删掉小括号和参数类型)
Func<int, int, int> m = (int i, int n) => i * n;
Console.WriteLine(m(8, 9));
Func<int, bool> zz = i => i > 0;
Console.WriteLine(zz(3));
}
输出结果:
5
wqfhqi
11
72
True
简单的Lambda表达式语句:(打印一个集合中大于10的数)
static void Main(string[] args)
{
int[] num = new int[] {2,5,432,324,435,89 };
//where方法会遍历集合中的每个元素,对于每个元素都调用
//a => a>10这个表达式判断一下是否为true
//如果为true,则把这个放到返回的集合中
IEnumerable<int> result = num.Where(a => a > 10);
foreach (int i in result) {
Console.WriteLine($"{i}");
}
}
Lambda常用扩展方法:
先定义一个装载数据的类
class Employee
{
public long Id { get; set; }
public string Name { get; set; }//姓名
public int Age { get; set; }//年龄
public bool Gender { get; set; }//性别
public int Salary { get; set; }//工资
public override string ToString()
{
return $"Id={Id},Name={Name},Age={Age},Gender={Gender},Salary={Salary},";
}
static void Main(string[] args)
{
List<Employee> list = new List<Employee>();
list.Add(new Employee { Id = 1, Name = "jerry", Age = 28, Gender = true, Salary = 5000 });
list.Add(new Employee { Id = 2, Name = "jim", Age = 33, Gender = true, Salary = 3000 });
list.Add(new Employee { Id = 3, Name = "lili", Age = 35, Gender = false, Salary = 9000 });
list.Add(new Employee { Id = 4, Name = "lucy", Age = 16, Gender = false, Salary = 2000 });
list.Add(new Employee { Id = 5, Name = "kimi", Age = 25, Gender = true, Salary = 1000 });
list.Add(new Employee { Id = 6, Name = "nancy", Age = 35, Gender = false, Salary = 8000 });
}
}
(1)Count方法和Any方法(当你只想看到是否含有数据时,建议使用Any方法):
//统计工资大于5000的人数
Console.WriteLine(list.Count(a => a.Salary >= 5000));
//统计性别为true且年龄大于25的人数
Console.WriteLine(list.Count(a=>a.Gender = true && a.Age >25));
//判断是否至少含有一条数据,返回结果为bool类型
Console.WriteLine(list.Any(a => a.Salary > 9000));
Console.WriteLine(list.Any(a => a.Salary < 9000));
返回结果:3
返回结果:4
返回结果:false
返回结果:true
(2)获取一条数据(是否带参数的两种写法)
//Single:有且只有一条满足要求的数据(没有数据或大于一条数据会抛异常)
Employee e1 = list.Where(a => a.Name == "kimi").Single();
Console.WriteLine(e1);
//SingleOrDefault:最多只有一条满足要求的数据
//First:至少有一条,返回第一条
Employee e2 = list.First(a => a.Age > 30);
Console.WriteLine(e2);
//FirstOrDefault:返回第一条或者默认值
Employee e3 = list.FirstOrDefault(a => a.Age > 40);
Console.WriteLine(e3==null);
返回结果:
Id=5,Name=kimi,Age=25,Gender=True,Salary=1000,
Id=2,Name=jim,Age=33,Gender=True,Salary=3000,
True
(3)按年龄进行排序(默认升序)
OrderBy:
IEnumerable<Employee> employees = list.OrderBy(a => a.Age);
foreach (var i in employees) {
Console.WriteLine(i);
}
返回结果:
Id=4,Name=lucy,Age=16,Gender=False,Salary=2000,
Id=5,Name=kimi,Age=25,Gender=True,Salary=1000,
Id=1,Name=jerry,Age=28,Gender=True,Salary=5000,
Id=2,Name=jim,Age=33,Gender=True,Salary=3000,
Id=3,Name=lili,Age=35,Gender=False,Salary=9000,
Id=6,Name=nancy,Age=35,Gender=False,Salary=8000,
按年龄进行OrderByDescending(降序):
IEnumerable<Employee> employees = list.OrderByDescending(a => a.Age);
foreach (var i in employees) {
Console.WriteLine(i);
}
返回结果:
Id=3,Name=lili,Age=35,Gender=False,Salary=9000,
Id=6,Name=nancy,Age=35,Gender=False,Salary=8000,
Id=2,Name=jim,Age=33,Gender=True,Salary=3000,
Id=1,Name=jerry,Age=28,Gender=True,Salary=5000,
Id=5,Name=kimi,Age=25,Gender=True,Salary=1000,
Id=4,Name=lucy,Age=16,Gender=False,Salary=2000,
OrderBy和ThenBy同时使用:
//先根据年龄进行降序排列,当年龄相等的再进行工资的升序排列
IEnumerable<Employee> e = list.OrderByDescending(a => a.Age).ThenBy(a=>a.Salary);
foreach (var i in e) {
Console.WriteLine(i);
}
运行结果:
Id=6,Name=nancy,Age=35,Gender=False,Salary=8000,
Id=3,Name=lili,Age=35,Gender=False,Salary=9000,
Id=2,Name=jim,Age=33,Gender=True,Salary=3000,
Id=1,Name=jerry,Age=28,Gender=True,Salary=5000,
Id=5,Name=kimi,Age=25,Gender=True,Salary=1000,
Id=4,Name=lucy,Age=16,Gender=False,Salary=2000,
(4)GroupBy(分组):对年龄进行分组,通过select的映射,显示出想要呈现的数据
var item1 = list.GroupBy(e => e.Age).Select(g => new
{
nianling = g.Key,
maxs = g.Max(e => e.Salary),
mins = g.Min(e => e.Salary),
renshu = g.Count()
});
foreach (var i in item1)
{
Console.WriteLine(i.nianling + "," + i.maxs + "," + i.mins + "," + i.renshu);
}
返回结果:
28,5000,5000,1
33,3000,3000,1
35,9000,8000,2
16,2000,2000,1
25,1000,1000,1