Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think this is what you're looking for...</p> <pre><code> internal class Section { public int SectionId { get; set; } public string Name { get; set; } public int SortOrder { get; set; } } internal class ContentPage { public int PageId { get; set; } public int SectionId { get; set; } public string Title { get; set; } public int SortOrder { get; set; } } static void Main(string[] args) { List&lt;Section&gt; sections = new List&lt;Section&gt;(); sections.Add(new Section() { SectionId = 1, Name = "One", SortOrder = 1 }); sections.Add(new Section() { SectionId = 2, Name = "Two", SortOrder = 3 }); sections.Add(new Section() { SectionId = 3, Name = "Three", SortOrder = 2 }); List&lt;ContentPage&gt; contentPages = new List&lt;ContentPage&gt;(); contentPages.Add(new ContentPage() { PageId = 11, SectionId = 1, Title = "Page One", SortOrder = 1 }); contentPages.Add(new ContentPage() { PageId = 12, SectionId = 1, Title = "Page Two", SortOrder = 3 }); contentPages.Add(new ContentPage() { PageId = 13, SectionId = 2, Title = "Page Three", SortOrder = 2 }); contentPages.Add(new ContentPage() { PageId = 16, SectionId = 2, Title = "Page Four", SortOrder = 4 }); contentPages.Add(new ContentPage() { PageId = 17, SectionId = 2, Title = "Page Eight", SortOrder = 5 }); contentPages.Add(new ContentPage() { PageId = 18, SectionId = 1, Title = "Page Ten", SortOrder = 6 }); var items = from section in sections orderby section.SortOrder join contentPage in (from contentPage in contentPages orderby contentPage.SortOrder group contentPage by contentPage.SectionId into grp select grp.FirstOrDefault()) on section.SectionId equals contentPage.SectionId select new { PageId = contentPage.PageId, SectionId = section.SectionId, SectionName = section.Name, Title = contentPage.Title, SortOrder = section.SortOrder }; foreach (var newItem in items) { Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}", newItem.PageId, newItem.SectionId, newItem.SectionName, newItem.Title, newItem.SortOrder)); } } </code></pre> <p>Note that the sample data you provided shows a sort order of 3 for section 2, but your sample results list its sort order as 2.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload