我正在使用linq2sql为论坛创建一些标记功能,我有两个表
[代码]
[ForumTagRelation]
我想像SO那样检索最受欢迎的标签。
我试过这样做:
List<Tag> popularTags = db.Tags.Select(x => x.ForumTagRelations.GroupBy(y => y.TagId).OrderByDescending(z => z.Count())).Take(count).ToList();
但这只会返回以下错误:
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?)
问题是我如何轻松返回在ForumTagRelation表中具有最多计数的标签列表?
答案 0 :(得分:1)
应该是这样:
List<Tag> popularTags = (db.Tags.Where(t => db.ForumTagRelations.GroupBy(gbftr => gbftr.TagID).OrderByDescending(obftr => obftr.Count()).Take(count).Any(ftr => ftr.Key == t.TagID)).Take(count)).ToList();