Entity Framework fetch Top 10 rows

末鹿安然 提交于 2019-12-22 10:31:38

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/17493132/entity-framework-fetch-top-10-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!