LINQ with Subquery/Group By/Join

424

Solution 1

Here's my first attempt at it:

from sectionPage in pages
group sectionPage by sectionPage.Section_ID into sectionGroup
join page in pages on sectionGroup.Key equals page.Section_ID
where page.SortOrder == sectionGroup.Min(p => p.SortOrder)
orderby page.SortOrder
select page;

What happens is first we create a group on the section id so that we can get the minimum sort order later. Next, we join a new reference to pages in on the section id, and filter by SortOrder being the minimum from the section group. Note, for simple expressions like the Min() call, I prefer the inline lambda expression over another query.

Finally, we add an orderby to order the pages, and we return the page (note you can change this to certain fields if you prefer).

Solution 2

I think this is what you're looking for...

    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<Section> sections = new List<Section>();
        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<ContentPage> contentPages = new List<ContentPage>();
        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));
        }
    }

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.

Share:
424
Arnau EC
Author by

Arnau EC

Updated on June 06, 2022

Comments

  • Arnau EC
    Arnau EC almost 2 years

    So I have the following code:

    <input type="radio" id="si" name="new-car" value="S" class="action-key ui-helper-hidden-accessible">
    <label for="si" class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-left ui-state-active" role="button" aria-disabled="false" aria-pressed="true"><span class="ui-button-text">Si</span></label>
    

    And I'm trying to click it using Selenium. I have tried with Selenium functions and it is not working and now I'm trying with:

    driver.execute_script("document.getElementById('si').checked = true;")
    

    But no luck either... Any thoughts? thx