Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# selectmany

作者:剑歌踏天下   发布日期:2026-02-09   浏览:94

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# 时间比较

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站