前面的章节细致介绍了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:
数据源2:
二、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、更新集合内的指定对象属性
这里是把原有的User集合中每个User的name属性提取出来,组成一个全是name的新的集合,故新集合为String属性。
/* 修改集合内所有医生的工资为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与传统集合方法的结合使用是非常重要的。