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

2024-02-03 10:33:10 浏览数 (1)

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

对集合的增删改查

Linq是对集合进行操作,这里列举对集合增删改查的常用方法。在学习之前,我们要做一些准备工作,我们需要创建User对象和包含User对象的集合,创建Salary对象和包含Salary对象的集合,作为后面查询和输出的数据源,参见这篇文章C#进阶之LINQ表达式总结完成准备工作。

数据源1:

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

数据源2:

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

① 新增集合内对象

代码语言:javascript复制
/* 新增一个任意属性的用户到集合 */
/* 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);
代码语言:javascript复制
/* 输出结果 */
{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}

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

代码语言:javascript复制
/* 修改集合内所有医生的工资为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; });
代码语言:javascript复制
/* 输出结果 */
{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}

③ 删除集合内指定对象

代码语言:javascript复制
/* 删除集合内所有职业为医生的用户对象 */

/* 推荐使用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);
}
代码语言:javascript复制
/* 输出结果 */
{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}

④ 查询集合内的对象

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

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

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

0 人点赞