You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将带分组的LINQ查询语法转换为方法语法的技术问询

Converting LINQ Query Syntax (with Join + OrderBy + Group) to Method Syntax

Got it, let's break this down step by step—grouping alongside joins and ordering can feel a bit opaque when switching from query to method syntax, so I'll walk through exactly how to map your code.

First, let's start with a complete version of your original query syntax (since it cut off mid-group clause; I'll assume you're grouping subchapters by their parent Chapter object and selecting a structured result):

List<ChapterDTO> chapters = new List<ChapterDTO>(); // Pre-populated
List<SubChapterDTO> subChapters = new List<SubChapterDTO>(); // Pre-populated

// Complete query syntax example
var result = from c in chapters
             join sc in subChapters on c.ID equals sc.ChapterID
             orderby c.Number
             group sc by c into chapterGroups
             select new 
             { 
                 Chapter = chapterGroups.Key, 
                 SubChapters = chapterGroups.ToList() 
             };

Here's the equivalent method syntax:

var result = chapters
    // Step 1: Join chapters to subchapters, keeping both objects in the result
    .Join(
        inner: subChapters,
        outerKeySelector: c => c.ID,
        innerKeySelector: sc => sc.ChapterID,
        resultSelector: (c, sc) => new { Chapter = c, SubChapter = sc }
    )
    // Step 2: Order by the chapter's Number property
    .OrderBy(x => x.Chapter.Number)
    // Step 3: Group the subchapters by their parent Chapter
    .GroupBy(
        keySelector: x => x.Chapter,
        elementSelector: x => x.SubChapter
    )
    // Step 4: Project into the final result shape
    .Select(g => new 
    { 
        Chapter = g.Key, 
        SubChapters = g.ToList() 
    });

Key Mapping Notes:

  • Join: The Join method requires four parameters: the inner collection (subChapters), the key from the outer collection (c.ID), the key from the inner collection (sc.ChapterID), and a selector that combines matching pairs into an anonymous type (so we can access both the chapter and subchapter later for ordering/grouping).
  • OrderBy: Directly maps to the orderby clause—we use the anonymous type's Chapter.Number property to sort.
  • GroupBy: Instead of group sc by c, we use GroupBy with two parameters: the key to group by (x.Chapter) and the elements to include in each group (x.SubChapter).
  • Select: Just like the query syntax, we project the group into a readable shape where g.Key is the original Chapter object, and g contains all matching subchapters.

If you meant to group by a specific Chapter property (e.g., DisplayText):

If your original query was grouping by a property like c.DisplayText instead of the entire Chapter object, adjust the GroupBy and Select like this:

var result = chapters
    .Join(subChapters, c => c.ID, sc => sc.ChapterID, (c, sc) => new { c, sc })
    .OrderBy(x => x.c.Number)
    .GroupBy(x => x.c.DisplayText, x => x.sc)
    .Select(g => new 
    { 
        ChapterDisplayName = g.Key, 
        SubChapters = g.ToList() 
    });

内容的提问来源于stack exchange,提问作者user1054922

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:27:10