前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#进阶-LINQ实现对集合的增删改查

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

原创
作者头像
Damon小智
发布2024-04-30 23:23:04
1410
发布2024-04-30 23:23:04
举报
文章被收录于专栏:C#全栈文档库

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


一、LINQ表达式学前准备

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

1、C#代码准备

C#类:

代码语言:dotnet
复制
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; } //职业
}
代码语言:dotnet
复制
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:

数据源2:


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

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

1、新增集合内对象

代码语言: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}

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

代码语言: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}

3、删除集合内指定对象

代码语言: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}

4、查询集合内的对象

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

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

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


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

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、LINQ表达式学前准备
    • 1、C#代码准备
      • 2、数据库准备
      • 二、LINQ对集合增删改查语法
        • 1、新增集合内对象
          • 2、更新集合内的指定对象属性
            • 3、删除集合内指定对象
              • 4、查询集合内的对象
              • 三、LINQ对集合增删改查总结
              相关产品与服务
              数据库
              云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档