2012-02-17 81 views
0

我有一个带有注释图像的aspx网站。表格的结构是:EntityFramework选择最多评论图片

Table name: Images 
int id (Primary Key) 
varcharmax path(path of the image) 

Table name: Comments 
int picid; (Linked to image id) 
Text text; 

我需要使用实体框架选择排名前10的评论图像。这怎么可能?

回答

1

应该是这样的:

var result = (from img in db.Images 
       order by img.Comments.Count() descending 
       select img).Take(10); 

或者,如果像我一样,你更喜欢其他的语法(方法)

var result = db.Images 
       .OrderByDescending(img => img.Comments.Count()) 
       .Take(10); 
+0

图像中没有Commnents计数器。 – user1216157 2012-02-17 14:00:12

+0

然后,您需要在实体模型中设置“图片”和“评论”之间的关联。提示:如果你的数据库有正确的FK关系,它会为你做到这一点。 – Jamiec 2012-02-17 14:16:50

0

这将选择前10名:

var result = (
    from i in db.Images 
    order by i.Comments.Count() descending 
    select i).Take(10);