当前位置: 首页> 汽车> 行情 > 企业信息_汕头h5建站模板_站长之家关键词查询_今日头条新闻最新

企业信息_汕头h5建站模板_站长之家关键词查询_今日头条新闻最新

时间:2025/7/10 7:41:03来源:https://blog.csdn.net/m0_67412019/article/details/147110525 浏览次数: 0次
企业信息_汕头h5建站模板_站长之家关键词查询_今日头条新闻最新

一、列表子项提取

class Program
{static void Main(){var students = new List<Student>{new Student { Name = "Alice", Courses = new List<string> { "Math", "Science" } },new Student { Name = "Bob", Courses = new List<string> { "English", "History" } },new Student { Name = "Charlie", Courses = new List<string> { "Art", "Music" } }};//注意:x.Courses的类型是List<string>,SelectMany提取后的结果类型不变,一样是List<string>List<string> course_list = students.SelectMany(x => x.Courses).ToList();//["Math", "Science", "English", "History", "Art", "Music"]}
}

二、双重for循环

class Program
{static void Main(){var students = new List<string> { "Alice", "Bob", "Charlie" };var grades = new List<int> { 90, 80, 85 };//x是students的子项,y是grades的子项,第二入参是x和y,出参可以自定义类型var result = students.SelectMany(x => grades, (x, y) => new { Name = x, Score = y });// 主列表.SelectMany(x => 次列表, 入参为x和y出参可自定义的委托)foreach (var item in result){Console.WriteLine($"{item.Name} :{item.Score}");}/*Alice :90Alice :80Alice :85Bob :90Bob :80Bob :85Charlie :90Charlie :80Charlie :85*/}
}

三、笛卡尔积(扩展)

C#高级:结合Linq的SelectMany方法实现笛卡尔积效果_c# selectmany-CSDN博客

关键字:企业信息_汕头h5建站模板_站长之家关键词查询_今日头条新闻最新

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: