c# List合并怎么写
时间: 2024-05-16 09:17:44 浏览: 8
要将两个List合并成一个List,可以使用List.AddRange()方法。这个方法将会把一个List中的所有元素添加到另一个List的末尾。
以下是示例代码:
```csharp
List<int> list1 = new List<int>() { 1, 2, 3 };
List<int> list2 = new List<int>() { 4, 5, 6 };
List<int> mergedList = new List<int>();
mergedList.AddRange(list1);
mergedList.AddRange(list2);
```
在上面的代码中,我们先创建了两个List `list1` 和 `list2`,然后创建了一个新的空List `mergedList`。使用`AddRange()`方法将`list1`和`list2`中的元素添加到`mergedList`中,最终得到了一个包含所有元素的List。
相关问题
C# list合并单元格
以下是C#中合并DataGridView单元格的方法:
```csharp
// 定义一个方法,用于合并指定列的相邻单元格
private void MergeCells(int columnIndex)
{
int rowCount = dataGridView1.Rows.Count;
int startIndex = 0; // 合并单元格的起始行索引
int mergeCount = 1; // 需要合并的单元格数量
for (int i = 1; i < rowCount; i++)
{
DataGridViewRow previousRow = dataGridView1.Rows[i - 1];
DataGridViewRow currentRow = dataGridView1.Rows[i];
// 如果当前单元格的值与上一个单元格的值相同,则需要合并
if (previousRow.Cells[columnIndex].Value.Equals(currentRow.Cells[columnIndex].Value))
{
mergeCount++;
}
else
{
// 合并单元格
dataGridView1.Rows[startIndex].Cells[columnIndex].RowSpan = mergeCount;
// 将需要合并的单元格的值设置为居中显示
for (int j = startIndex + 1; j < startIndex + mergeCount; j++)
{
dataGridView1.Rows[j].Cells[columnIndex].Value = "";
dataGridView1.Rows[j].Cells[columnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
// 重置起始行索引和需要合并的单元格数量
startIndex = i;
mergeCount = 1;
}
}
// 处理最后一组需要合并的单元格
dataGridView1.Rows[startIndex].Cells[columnIndex].RowSpan = mergeCount;
// 将需要合并的单元格的值设置为居中显示
for (int j = startIndex + 1; j < startIndex + mergeCount; j++)
{
dataGridView1.Rows[j].Cells[columnIndex].Value = "";
dataGridView1.Rows[j].Cells[columnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
}
```
调用上述方法,可以实现对指定列的相邻单元格进行合并:
```csharp
MergeCells(0); // 合并第一列的相邻单元格
```
c# 合并多个List
在 C# 中,可以使用 `List.AddRange()` 方法将多个 List 合并成一个 List。以下是一个示例:
```csharp
List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int> { 4, 5, 6 };
List<int> list3 = new List<int> { 7, 8, 9 };
List<int> mergedList = new List<int>();
mergedList.AddRange(list1);
mergedList.AddRange(list2);
mergedList.AddRange(list3);
// mergedList 现在包含所有元素:1, 2, 3, 4, 5, 6, 7, 8, 9
```
另外,如果你有多个 List,可以将它们放在一个 List 的数组中,然后使用 LINQ 的 `SelectMany()` 方法合并它们。以下是示例:
```csharp
List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int> { 4, 5, 6 };
List<int> list3 = new List<int> { 7, 8, 9 };
List<int>[] lists = new List<int>[] { list1, list2, list3 };
List<int> mergedList = lists.SelectMany(x => x).ToList();
// mergedList 现在包含所有元素:1, 2, 3, 4, 5, 6, 7, 8, 9
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)