C# Lambda 表达式 和 LinQ


一、Lambda 表达式

1. 定义

简单来讲就是匿名函数,我们不声明方法名,只写方法体,这个方法体就是Lambda表达式

2. 用法

Action action1 = () => { Console.WriteLine($"Lambda ==> 无参无返回值!"); };
action1.Invoke();

Action<string> action2 = (string name) => { Console.WriteLine($"Lambda ==> 一个参数(参数1 = {name}),无返回值"); };
action2.Invoke("字符");

Action<string, int> action3 = (string name, int num) => { Console.WriteLine($"Lambda ==> 两个参数(参数1 = {name}, 参数2 = {num}),无返回值"); };
action3.Invoke("字符", 12);

// 1. 单参数简化,可以直接去掉括号和类型定义,因为在泛型中已经定义
Action<string> action4 =  name => { Console.WriteLine($"Lambda ==> 一个参数(参数1 = {name}),无返回值"); };
action4.Invoke("字符");

// 2. 去掉括号中的类型定义,因为在泛型中已经定义
Action<string, int> action5 = (name, num) => { Console.WriteLine($"Lambda ==> 两个参数(参数1 = {name}, 参数2 = {num}),无返回值"); };
action5.Invoke("字符", 12);

二、LinQ

1. 定义

语言集成查询,可以对对象集合、数据源的集成式查询操作

2. 用法

新建一个Student类

3. 语法

查找:Where

Console.WriteLine("查找大于24岁的学生");
var moreThen24 = students.Where(stu => stu.Age > 24).ToList();

分组:GroupBy

Console.WriteLine("按照班级进行分组");
var groupClass = students.GroupBy(stu => stu.Class).ToList();

排序:OrderBy & OrderByDescending

Console.WriteLine("按照Id升序");
var orderByIdAsc = students.OrderBy(stu=>stu.StudentId).ToList();
Console.WriteLine("按照Id降序");
var orderByIdDec = students.OrderByDescending(stu => stu.StudentId).ToList();

查找第一个或最后一个数据:First & Last

Student student1 = students.First();
Student student2 = students.First(stu => stu.Class == "一班");

数据是否满足条件:Any & All

Console.WriteLine("是否存在三班");
bool IsClass = students.Any(stu => { return stu.Class == "三班"; });
Console.WriteLine("是否所有学生都大于19岁");
bool IsAll = students.All(stu => { return stu.Age > 19; }); 

忽略:Skip

Console.WriteLine("不保留前三个学生");
List<Student> students3 = students.Skip(3).ToList();
Console.WriteLine("忽略满足条件的数据直至不满足开始");
List<Student> students4 = students.SkipWhile(stu => { return stu.Class == "二班"; }).ToList();

选取几个元素:Task

Console.WriteLine("选取三个开始");
List<Student> students5 = students.Take(3).ToList();
Console.WriteLine("选择直至满足条件的数据开始");
List<Student> students6 = students.TakeWhile(stu => { return stu.Class == "二班"; }).ToList();

选取:Select
筛选字段,组成新的元素对象,为了展示可以通过类去展示对象,此处可以新建一个StudentBo的类,就可以直接筛选出对象,不再是匿名的集合对象

Console.WriteLine("选出班级和姓名");
List<StudentBo> studentBos = students.Select(stu => new { stu.Name, stu.Class }).ToList();

简单运算:Max、Min、Average、Sum、Contains、StartWith、Count & LongCount

... 【狗头】

联表查询:Join

class Product
{
    public string? Name { get; set; }
    public int CategoryId { get; set; }
}

class Category
{
    public int Id { get; set; }
    public string? CategoryName { get; set; }
}

class JoinInfo 
{
    public string Name { get; set; }
    public string CategoryName { get; set; }
}

public static void Example()
{
    List<Product> products = new List<Product>
    {
        new Product { Name = "Cola", CategoryId = 0 },
        new Product { Name = "Tea", CategoryId = 0 },
        new Product { Name = "Apple", CategoryId = 1 },
        new Product { Name = "Kiwi", CategoryId = 1 },
        new Product { Name = "Carrot", CategoryId = 2 },
    };

    List<Category> categories = new List<Category>
    {
        new Category { Id = 0, CategoryName = "Beverage" },
        new Category { Id = 1, CategoryName = "Fruit" },
        new Category { Id = 2, CategoryName = "Vegetable" }
    };

    Console.WriteLine("查询表达式");
	// Join products and categories based on CategoryId
	var query = from product in products
			join category in categories on product.CategoryId equals category.Id
			select new { product.Name, category.CategoryName };

	foreach (var item in query)
	{
		Console.WriteLine($"{item.Name} - {item.CategoryName}");
	}

	Console.WriteLine("流式查询");
	var resut = products.Join(categories, product => product.CategoryId, Category => Category.Id, 	(product, category) => { return new JoinInfo { Name = product.Name, CategoryName = category.CategoryName }; });
	foreach (var item in resut)
	{
		Console.WriteLine($"{item.Name} - {item.CategoryName}");
	}
	// 调用ToList() 可以不需要JoinInfo类
	resut = products.Join(categories, product => product.CategoryId, Category => Category.Id, (product, category) => { return new { product.Name, category.CategoryName }; }).ToList();

    // This code produces the following output:
    //
    // 查询表达式
	// Cola - Beverage
	// Tea - Beverage
	// Apple - Fruit
	// Kiwi - Fruit
	// Carrot - Vegetable
	// 流式查询
	// Cola - Beverage
	// Tea - Beverage
	// Apple - Fruit
	// Kiwi - Fruit
	// Carrot - Vegetable
}

附录:查询对象集合(官方文档)

写在最后

本博文只是我在学习c#的过程中所做的笔记,方便以后查阅实现过程。资料均来自网上,如果有侵权请联系我删除,谢谢。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值