LINQ(语言集成查询-language intergrated query)是一款很常用的扩展包,支持C#和Java,在系统进行查询数据的动作时,相较于执行数据库层面的SQL语句,后端层面的LINQ运行起来会更加高效稳定,可以极大缩短每次与数据库交互的时间,增加系统功能的稳定性,提高查询效率。本篇文章是LINQ的使用教程,用LINQ模仿对应SQL语句的执行效果。
目录:
章节一:基础语法Ⅰ(Select、OrderBy、Count、Average、Sum、Max、Min)
章节二:基础语法Ⅱ(Any、All、Single、First、Last、Skip、Take、Top)
章节三:分组查询 (GroupBy)
章节五:多表查询 Ⅱ(Join连接查询)
章节六:投影
章节七:LINQ实现对集合的增删改查
准备工作:
我们创建User对象和包含User对象的集合,后面以此为例。
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"}
};

后面大家根据自己的需要跳转到各个章节学习!
额外准备工作(章节四、五、七):
class Salary
{
public int id { get; set; }
public int user_id { get; set; }
public string name { get; set; }
public double salary { get; set; }
public bool active { get; set; }
public string occupation { get; set; } //职业
}
List<Salary> list = new List<Salary>()
{
new Salary { id = 1, user_id = 1, name = "Zhang Long", salary = 7800, active = true, occupation = "Teacher"},
new Salary { id = 2, user_id = 2, name = "Zhang Jin", salary = 1500, active = true, occupation = "Student"},
new Salary { id = 3, user_id = 3, name = "Zhang Shuai", salary = 8800, active = false, occupation = "Teacher"},
new Salary { id = 4, user_id = 4, name = "Liu Guangzhi", salary = 12800, active = true, occupation = "Doctor"},
new Salary { id = 5, user_id = 5, name = "Liu Ziming", salary = 13600, active = true, occupation = "Doctor"},
new Salary { id = 6, user_id = 6, name = "Liu Shuai", salary = 29000, active = false, occupation = "Doctor"},
new Salary { id = 7, user_id = 7, name = "Liu Jin", salary = 7000, active = true, occupation = "Builder"},
new Salary { id = 8, user_id = 8, name = "Jiang Long", salary = 8500, active = false, occupation = "Builder"},
new Salary { id = 9, user_id = 9, name = "Hu Ziming", salary = 2100, active = true, occupation = "Student"},
new Salary { id = 10, user_id = 10, name = "Hu Jin", salary = 1300, active = true, occupation = "Student"}
};


Damon, Chinese, Liu Guangzhi, Software development engineer, CSDN quality creator, Ali Cloud expert blogger, Microsoft Technology Associate, Good at C#, Java, PHP, Python, etc, Love sports, Workaholic, Communist.