Groupby in relationtable

Posted by Dofs on Stack Overflow See other posts from Stack Overflow or by Dofs
Published on 2010-04-04T09:11:15Z Indexed on 2010/04/04 9:13 UTC
Read the original article Hit count: 291

Filed under:

I am creating some tag functionality for a forum using linq2sql, and I have two tables

[Tag]

  • TagId
  • TagName

[ForumTagRelation]

  • TagId
  • ForumId

I would like to retrieve, like SO, the most popular tags.

I have tried to do this by:

 List<Tag> popularTags = db.Tags.Select(x => x.ForumTagRelations.GroupBy(y => y.TagId).OrderByDescending(z => z.Count())).Take(count).ToList();

But this just returns the following error:

Error   1   Cannot implicitly convert type 'System.Collections.Generic.List<System.Linq.IOrderedEnumerable<System.Linq.IGrouping<System.Guid?,SampleWebsite.ForumTagRelation>>>' to 'System.Collections.Generic.IEnumerable<SampleWebsite.Tag>'. An explicit conversion exists (are you missing a cast?)    

The question is how I easily can return a list of tags which has the most counts in the ForumTagRelation table?

© Stack Overflow or respective owner

Related posts about linq2sql