C#进阶-LINQ实现对集合的增删改查

举报
Damon小智 发表于 2024/04/30 23:26:41 2024/04/30
【摘要】 前面的章节细致介绍了LINQ扩展包的具体方法使用,本篇则是演示LINQ在日常开发中的常用操作,实现结果集的增删改查。目前LINQ支持两种语法,我会在每个案例前先用大家熟知的SQL语句表达,再在后面用C#的两种LINQ语法分别实现。LINQ语法第一次接触难免感到陌生,最好的学习方式就是在项目中多去使用,相信会有很多感悟。

一、LINQ表达式学前准备

在学习之前,我们要做一些准备工作,我们需要创建User对象和包含User对象的集合,作为后面查询和输出的数据源。

1、C#代码准备

C#类:

class User
{
    public int id { get; set; } 
    public string name { get; set; } 
    public bool gender { get; set; }//male: true; female: fasle
    public int age { get; set; }
    public string occupation { get; set; } //职业
}
List<User> list = new List<User>()
{
    new User { id = 1, name = "Zhang Long", age = 38, gender = true, occupation = "Teacher"},
    new User { id = 2, name = "Zhang Jin", age = 18, gender = false, occupation = "Student"},
    new User { id = 3, name = "Zhang Shuai", age = 38, gender = false, occupation = "Teacher"},
    new User { id = 4, name = "Liu Guangzhi", age = 38, gender = false, occupation = "Doctor"},
    new User { id = 5, name = "Liu Ziming", age = 38, gender = true, occupation = "Doctor"},
    new User { id = 6, name = "Liu Shuai", age = 29, gender = false, occupation = "Doctor"},
    new User { id = 7, name = "Liu Jin", age = 21, gender = true, occupation = "Builder"},
    new User { id = 8, name = "Jiang Long", age = 38, gender = true, occupation = "Builder"},
    new User { id = 9, name = "Hu Ziming", age = 21, gender = true, occupation = "Student"},
    new User { id = 10, name = "Hu Jin", age = 21, gender = false, occupation = "Student"}
};

2、数据库准备

数据源1:

image.png

数据源2:

image.png


二、LINQ对集合增删改查语法

Linq是对集合进行操作,这里列举对集合增删改查的常用方法。在学习之前,我们要做一些准备工作,我们需要创建User对象和包含User对象的集合,创建Salary对象和包含Salary对象的集合,作为后面查询和输出的数据源。

1、新增集合内对象

/* 新增一个任意属性的用户到集合 */
/* C#版本1 */
 User userAdd = new User() {
  id = 11,
  name = "Liu Mingxiu",
  age = 22,
  gender = false,
  occupation = "Doctor"
};
list.Add(userAdd);

/* C#版本2(支持一次添加多个) */
IEnumerable<User> userAddList = new List<User>().Append(userAdd);
list.AddRange(userAddList);
/* 输出结果 */
{id = 1, name = Zhang Long, age = 38, gender = True, occupation = Teacher}, 
{id = 2, name = Zhang Jin, age = 18, gender = False, occupation = Student}, 
{id = 3, name = Zhang Shuai, age = 38, gender = False, occupation = Teacher}, 
{id = 4, name = Liu Guangzhi, age = 38, gender = False, occupation = Doctor}, 
{id = 5, name = Liu Ziming, age = 38, gender = True, occupation = Doctor}, 
{id = 6, name = Liu Shuai, age = 29, gender = False, occupation = Doctor}, 
{id = 7, name = Liu Jin, age = 21, gender = True, occupation = Builder}, 
{id = 8, name = Jiang Long, age = 38, gender = True, occupation = Builder}, 
{id = 9, name = Hu Ziming, age = 21, gender = True, occupation = Student}, 
{id = 10, name = Hu Jin, age = 21, gender = False, occupation = Student}, 
{id = 11, name = Liu Mingxiu, age = 22, gender = False, occupation = Doctor}

2、更新集合内的指定对象属性

/* 修改集合内所有医生的工资为10000且在职 */
/* C#版本1 使用ForEach方法 */
salaryList.Where(item => item.occupation == "Doctor").ToList()
                .ForEach(u => { u.salary = 10000; u.active = true; });

/* C#版本2 使用All方法(需要返回true) */
salaryList.Where(item => item.occupation == "Doctor").ToList()
                .All( u => { u.salary = 10000; u.active = true; return true; });
/* 输出结果 */
{id = 1, name = Zhang Long, occupation = Teacher, active = True, salary = 7800}
{id = 2, name = Zhang Jin, occupation = Student, active = True, salary = 1500}
{id = 3, name = Zhang Shuai, occupation = Teacher, active = False, salary = 8800}
{id = 4, name = Liu Guangzhi, occupation = Doctor, active = True, salary = 10000}
{id = 5, name = Liu Ziming, occupation = Doctor, active = True, salary = 10000}
{id = 6, name = Liu Shuai, occupation = Doctor, active = True, salary = 10000}
{id = 7, name = Liu Jin, occupation = Builder, active = True, salary = 7000}
{id = 8, name = Jiang Long, occupation = Builder, active = False, salary = 8500}
{id = 9, name = Hu Ziming, occupation = Student, active = True, salary = 2100}
{id = 10, name = Hu Jin, occupation = Student, active = True, salary = 1300}

3、删除集合内指定对象

/* 删除集合内所有职业为医生的用户对象 */

/* 推荐使用RemoveAll方法批量删除 */
/* C#版本1 */
list.RemoveAll(item => item.occupation == "Doctor");

/* 也可以使用Remove方法单个删除 */
/* C#版本2 */
List<User> maps = list.Where(item => item.occupation == "Doctor").ToList();
foreach (User userDelete in maps){
  list.Remove(userDelete);
}

/* C#版本3 */
List<User> maps = (from u in list where u.occupation == "Doctor" select u).ToList();
foreach (User userDelete in maps){
  list.Remove(userDelete);
}
/* 输出结果 */
{id = 1, name = Zhang Long, age = 38, gender = True, occupation = Teacher}, 
{id = 2, name = Zhang Jin, age = 18, gender = False, occupation = Student}, 
{id = 3, name = Zhang Shuai, age = 38, gender = False, occupation = Teacher}, 
{id = 7, name = Liu Jin, age = 21, gender = True, occupation = Builder}, 
{id = 8, name = Jiang Long, age = 38, gender = True, occupation = Builder}, 
{id = 9, name = Hu Ziming, age = 21, gender = True, occupation = Student}, 
{id = 10, name = Hu Jin, age = 21, gender = False, occupation = Student}

4、查询集合内的对象

基础查询参考:C#进阶-LINQ表达式基础语法

多表查询参考:多表查询 Ⅰ(交集、并集、差集、去重)多表查询 Ⅱ(Join连接查询)

分组查询参考:分组查询 (GroupBy)


三、LINQ对集合增删改查总结

LINQ(Language Integrated Query)提供了强大的查询能力,主要用于读取和检索数据集合中的信息。虽然LINQ自身不直接支持修改操作(增、删、改),但它可以与其他.NET功能如List<T>的方法配合使用,来实现集合的完整管理。下面将详细介绍如何结合LINQ进行集合的增删改查操作。通过与.NET的集合操作结合,LINQ可以实现对数据集合的全面管理。这种方式的优势在于能够利用LINQ强大的查询能力来选择操作的目标,从而使数据操作更加精确和高效。在实际应用中,理解LINQ与传统集合方法的结合使用是非常重要的。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。