我有两个表“POSTS”和“COMMENTS”。一个帖子可以有很多评论,我希望能够选择评论数量最多的前 10 个帖子。 post_id 是评论表中的 FK。我正在使用 Linq to SQL。请告诉我如何做到这一点。提前致谢。
编辑
var top = (from q in db.question_tables
from a in db.answer_tables
where q.QUEST_ID.Equals(a.ANS_QUEST_ID)
orderby q.QUEST_TEXT.Count() descending
select new
{
QUEST_TEXT = q.QUEST_TEXT
}).Take(10);
这就是我的 linq 查询现在的样子,它给出了一个错误“Sequence operators not supported for type 'System.String'.”。 :/
最佳答案
该错误消息是因为您在字符串属性 (QUEST_TEXT
) 上调用 .Count()
。编译是因为字符串是可枚举的。但是,Linq-to-SQL 不理解这一点。
如果您的 DBML 文件中映射了两个表之间的关系,那么您可以在表达式中使用它:
var top = (from q in db.question_tables
orderby q.answers.Count() descending
select q).Take(10);
但是,您发布的代码与您提供的描述并不完全相符。你提到评论,但代码谈论答案。
关于c# - linq 查询从表中选择评论最多的前 10 个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8702456/