c# - entity framework linq query, get top N postIDs from favorite's table based on count -
i have database keeping track of favorites
looks like
[id] [postid] [userid] [datefavorited]
i'm attempting write entity framework query in linq 12 postids
appear in database.
i've looked @ documentation, i'm not putting how this. purpose "most favorited" page
i feel there elegant solution, i've frustrated myself point can't think of way without feting entire table that's bad idea.
in sql, be:
select top 12 postid, count(*) favcount favorites group postid order favcount desc
in linq, believe be:
var ret = db.favorites.groupby( fav => fav.postid ).select( favgroup => new { postid = favgroup.key, count = favgroup.count() } ).orderby( row => row.count ).take( 12 );
with type of ret
being ienumerable<anonymous{ postid, count }>
.