Skip to content

Commit

Permalink
doc: update docs/cs.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Apr 12, 2024
1 parent d8e1bdf commit e8af723
Showing 1 changed file with 38 additions and 43 deletions.
81 changes: 38 additions & 43 deletions docs/cs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1065,23 +1065,17 @@ bool hasKey2 = hashTable.ContainsKey("key2");
string valueOfKey2 = (string)hashTable["key2"];
```



LINQ
--------

> C#语言中的LINQ(Language-Integrated Query)是一种强大的查询语言,它提供了一种统一的编程模型,使得数据查询变得更加直观和简洁。


### FROM

<!--rehype:wrap-class=col-span-3-->

> 任何数据源,包括对象集合、数据库、XML等。


### WHERE

<!--rehype:wrap-class=col-span-2&style=background:#e91e63;-->
Expand Down Expand Up @@ -1115,8 +1109,6 @@ List<string> nameList = new() { "Bob", "Echo" };
var result4 = students.Where(student => nameList.Contains(student.Name));
```



### GROUPBY

> 分组查询
Expand All @@ -1140,8 +1132,6 @@ var groupedByGrade = students.GroupBy(student => student.Grade);

<!--rehype:className=wrap-text-->



### SELECT

<!--rehype:wrap-class=col-span-2&style=background:#e91e63;-->
Expand All @@ -1163,9 +1153,9 @@ List<Student> students = new List<Student>

// 使用SELECT创建一个新的匿名类,并输出为集合,一般配合Where使用
var result1 = students.Select(student =>
new
new
{
student.Name,
student.Name,
student.Age
});

Expand All @@ -1177,8 +1167,6 @@ var result2 = students.Select(student => new StudentDto()
});
```



### ORDERBY

> 排序
Expand All @@ -1205,53 +1193,58 @@ var result2 = students.OrderByDescending(student => student.Age);

<!--rehype:className=wrap-text-->



### JOIN

<!--rehype:wrap-class=col-span-3-->

> `Join`:用于执行内连接操作,它会返回两个数据源中满足连接条件的元素的交集
>
> `GroupJoin`:用于执行左外连接(left outer join)操作,它会返回左边数据源的所有元素,以及每个元素所匹配的右边数据源的元素组成的集合。(嵌套)
- `Join`:用于执行内连接操作,它会返回两个数据源中满足连接条件的元素的交集
- `GroupJoin`:用于执行左外连接(left outer join)操作,它会返回左边数据源的所有元素,以及每个元素所匹配的右边数据源的元素组成的集合。(嵌套)

示例数据源

```cs
// 示例数据源
List<Department> departments = new List<Department>
{
new Department { ID = 1, Name = "HR" },
new Department { ID = 2, Name = "IT" }
};
// 示例数据源
```

示例数据源

```cs
List<Employee> employees = new List<Employee>
{
new Employee { DepartmentID = 1, Name = "Alice" },
new Employee { DepartmentID = 2, Name = "Bob" },
new Employee { DepartmentID = 1, Name = "Charlie" },
new Employee { DepartmentID = 3, Name = "David" }
};
```

// 使用Join,将部门和员工相结合,获取部门名称和员工名称的集合
var joinQuery = departments.Join(employees,
department => department.ID, employee => employee.DepartmentID,
(department, employee) => new { Department = department.Name, Employee = employee.Name }
);
使用 `Join`,将部门和员工相结合,获取部门名称和员工名称的集合

// 使用GroupJoin,将部门和员工相结合,返回所有的部门,并返回每个部门相关联的员工集合(嵌套)
var groupJoinQuery = departments.GroupJoin(employees,
department => department.ID, employee => employee.DepartmentID,
(department, employeeGroup) => new
{
Department = department.Name,
Employees = employeeGroup.Select(e => e.Name).ToList()
}
);
```cs
var joinQuery = departments.Join(employees,
department => department.ID, employee => employee.DepartmentID,
(department, employee) => new { Department = department.Name, Employee = employee.Name }
);
```

使用 `GroupJoin`,将部门和员工相结合,返回所有的部门,并返回每个部门相关联的员工集合(嵌套)

```cs
var groupJoinQuery = departments.GroupJoin(employees,
department => department.ID, employee => employee.DepartmentID,
(department, employeeGroup) => new
{
Department = department.Name,
Employees = employeeGroup.Select(e => e.Name).ToList()
}
);
```

### 结果转换

<!--rehype:wrap-class=col-span-2&style=background:#e91e63;-->

```cs
Expand All @@ -1272,8 +1265,6 @@ Student firstStudent = result.First();
Student firstStudent = result.FirstOrDefault();
```



### 自定义扩展方法

```cs
Expand All @@ -1293,14 +1284,12 @@ public static class CustomExtensions
// 使用自定义扩展方法
var filteredData = students.CustomFilter(s => s.Age > 20);
```

<!--rehype:className=wrap-text-->

### 示例

<!--rehype:wrap-class=col-span-3&style=background:#e91e63;-->

> 假设有一个包含学生信息的列表,每个学生有姓名、年龄和成绩。使用LINQ查询来选择年龄大于20岁的学生,然后按照他们的成绩进行分组,并选择每个分组中年龄最小的学生的姓名。
假设有一个包含学生信息的列表,每个学生有姓名、年龄和成绩。使用LINQ查询来选择年龄大于20岁的学生,然后按照他们的成绩进行分组,并选择每个分组中年龄最小的学生的姓名。

```cs
// 示例数据源
Expand All @@ -1314,15 +1303,21 @@ List<Student> students = new List<Student>
new Student { Name = "Damon", Age = 28, Grade = "B" },
new Student { Name = "Echo", Age = 18, Grade = "C" }
};
```

// 使用LINQ进行查询
使用 `LINQ` 进行查询

```cs
var result = students
.Where(student => student.Age > 20) // WHERE: 选择年龄大于20的学生
.GroupBy(student => student.Grade) // GROUP BY: 按成绩分组
.Select(group => group.OrderBy(student => student.Age).First().Name) // SELECT: 选择每个分组中年龄最小的学生的姓名
.ToList(); //转换为List<Student>()
```

//输出结果
输出结果

```cs
["Charlie","Damon","David"]
```

Expand Down

0 comments on commit e8af723

Please sign in to comment.