using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// 示例数据:一个包含多个列表的列表
List<List<int>> listOfLists = new List<List<int>>()
{
new List<int> { 1, 2, 3 },
new List<int> { 4, 5 },
new List<int> { 6, 7, 8, 9 }
};
// 使用 SelectMany 将多个列表展平为一个单一的列表
var flattenedList = listOfLists.SelectMany(innerList => innerList).ToList();
// 输出结果
Console.WriteLine("Flattened List: " + string.Join(", ", flattenedList));
}
}
SelectMany 是 LINQ 中的一个方法,用于将多个集合展平为一个单一的集合。List<int> 的 List<List<int>>。通过使用 SelectMany,我们可以将这些内部列表中的所有元素合并到一个单一的 List<int> 中。innerList => innerList 表示我们将每个内部列表投影为其自身,从而将其所有元素合并到一个结果集中。ToList() 将结果转换为一个 List<int> 并输出。如果你需要更多关于 SelectMany 的用法或有其他问题,请告诉我!
上一篇:c#矩阵运算
下一篇:c# 时间比较
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站