拿支香烟戳地球 發表於 2026-1-9 10:44:17

C# LINQ SelectMany方法详解

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>1. 基本用法</li><li>2. 带索引的 SelectMany</li><li>3. 实际应用场景</li><ul class="second_class_ul"><li>一对多关系展平</li><li>字符串处理</li></ul><li>4. 查询语法</li><ul class="second_class_ul"></ul><li>5. 高级用法</li><ul class="second_class_ul"><li>条件过滤</li><li>多层展平</li></ul><li>注意事项</li><ul class="second_class_ul"></ul></ul></div><p>SelectMany 是 LINQ 中用于展平集合的强大操作符。让我们详细了解它的使用</p>
<p class="maodian"></p><h2>1. 基本用法</h2>
<div class="jb51code"><pre class="brush:csharp;">// 基础示例
var lists = new List&lt;List&lt;int&gt;&gt; {
    new List&lt;int&gt; { 1, 2, 3 },
    new List&lt;int&gt; { 4, 5, 6 }
};

var flattened = lists.SelectMany(x =&gt; x);
// 结果: </pre></div>
<p class="maodian"></p><h2>2. 带索引的 SelectMany</h2>
<div class="jb51code"><pre class="brush:csharp;">var result = lists.SelectMany((list, index) =&gt;
    list.Select(item =&gt; $"列表{index}: {item}"));</pre></div>
<p class="maodian"></p><h2>3. 实际应用场景</h2>
<p class="maodian"></p><h3>一对多关系展平</h3>
<div class="jb51code"><pre class="brush:csharp;">public class Student
{
    public string Name { get; set; }
    public List&lt;Course&gt; Courses { get; set; }
}

// 获取所有学生的所有课程
var allCourses = students.SelectMany(s =&gt; s.Courses);

// 带学生信息的课程列表
var studentCourses = students.SelectMany(
    student =&gt; student.Courses,
    (student, course) =&gt; new {
      StudentName = student.Name,
      CourseName = course.Name
    }
);</pre></div>
<p class="maodian"></p><h3>字符串处理</h3>
<div class="jb51code"><pre class="brush:csharp;">string[] words = { "Hello", "World" };
var letters = words.SelectMany(word =&gt; word.ToLower());
// 结果: ['h','e','l','l','o','w','o','r','l','d']</pre></div>
<p class="maodian"></p><h2>4. 查询语法</h2>
<div class="jb51code"><pre class="brush:csharp;">// 方法语法
var result = students.SelectMany(s =&gt; s.Courses);

// 等价的查询语法
var result = from student in students
            from course in student.Courses
            select course;</pre></div>
<p class="maodian"></p><h2>5. 高级用法</h2>
<p class="maodian"></p><h3>条件过滤</h3>
<div class="jb51code"><pre class="brush:csharp;">var result = students.SelectMany(
    student =&gt; student.Courses.Where(c =&gt; c.Credits &gt; 3),
    (student, course) =&gt; new {
      Student = student.Name,
      Course = course.Name,
      Credits = course.Credits
    });</pre></div>
<p class="maodian"></p><h3>多层展平</h3>
<div class="jb51code"><pre class="brush:csharp;">var departments = new List&lt;Department&gt;();
var result = departments
    .SelectMany(d =&gt; d.Teams)
    .SelectMany(t =&gt; t.Employees);</pre></div>
<p class="maodian"></p><h2>注意事项</h2>
<blockquote><p>性能考虑</p>
<p>- SelectMany 会创建新的集合<br />- 大数据量时注意内存使用<br />- 考虑使用延迟执行</p></blockquote>
<p>&nbsp;空值处理</p>
<div class="jb51code"><pre class="brush:csharp;">// 处理可能为null的集合
var result = students.SelectMany(s =&gt;
    s.Courses ?? Enumerable.Empty&lt;Course&gt;());</pre></div>
<blockquote><p>常见错误<br />- 忘记处理空集合<br />- 嵌套 SelectMany 过深<br />- 返回类型不匹配</p></blockquote>
<p>&nbsp;SelectMany 在处理嵌套集合、一对多关系时非常有用,掌握它可以大大简化复杂数据处理的代码</p>
<p>到此这篇关于C# LINQ SelectMany方法详解的文章就介绍到这了,更多相关C# LINQ SelectMany内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>C#中LINQ的Select与SelectMany函数使用</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: C# LINQ SelectMany方法详解