Entity Framework fetch Top 10 rows

21,437

If you wanted to find the top 10 Videos with the most number of Tags, you would probably find it easier, but in fact what you want to do now is exactly the same. You just need the top 10 Tags with the most number of Videos. Use this:

var mostUsedTags = db.Tags.OrderByDescending(t => t.Videos.Count).Take(10);
Share:
21,437
Sergey Sypalo
Author by

Sergey Sypalo

Microsoft Certified Solutions Expert, IT Professional and Systems Engineer in wide range of products with over a 10 years of hands-on experience. I'm expert in planning, testing, configuring, designing and deploying Active Directory, virtualization, messaging, systems management and monitoring servers, including multi-tiered antivirus and backup solutions in large complex networks.

Updated on July 06, 2020

Comments

  • Sergey Sypalo
    Sergey Sypalo almost 4 years

    I have a 3 tables in SQL database

    tblVideos:

    VideoID     int PK
    Title       varchar(100)
    Decription  varchar(100)
    

    tblTags:

    TagID       int PK
    TagText     varchar(100)
    

    tblVideosToTags:

    VideoID     int PK, FK to Videos
    TagID       int PK, FK to Tags
    

    In Entity Framework (v6-latest-nightly-build) I have 2 classes Video and Tag with many-to-many relationships. I need a help with building a LINQ to Entities or LINQ to SQL query which meets following conditions:

    Top 10 records from Tags, which is mostly used. So probably some summing/count/grouping needed