-1

我有一個由asp.net mvc和實體框架工作代碼首先創建的博客 我想通過兩種方式在頁面內以兩種方式顯示排名靠前的N(例如前10位)帖子將在實體框架中計算訂單

1-此規則

點 - (DateTime.Now - CreationDate)

點和CreationDate是表後的實體

(我想前N個NE大多數點帖子WES)

2,前N newes後,大多數評論

如何讓這兩個查詢由LINQ到實體?

public class Post 
{ 

    public int Id { get; set; } 

    public string Subject { get; set; } 

    public string Description { get; set; } 

    public DateTime CreationDate { get; set; } 

    public int Point { get; set; } 

    public virtual IList<Comment> Comments { get; set; } 


} 




    public class Comment 
    { 
     public int Id { get; set; } 

     public string Name { get; set; } 

     public string Description { get; set; } 

     public Virtual Post Post { get; set;} 
    } 

編輯 我想在查看後的順序點和CreationDate 這樣的查詢的功能:

var latestPosts = dbContext.Posts 
    .OrderByDescending(p => p.Point - (DateTime.Now - p.CreationDate).TotalDays) 
    .Take(postCount)ToList(); 

但它引發錯誤

+0

聽起來你需要'OrderBy()',但你的問題有點不清楚。 – gunr2171

+0

你到目前爲止有什麼? – Tico

回答

0

這裏有兩個疑問您的問題:

獲取基於CreationDate

var latestPosts = dbContext.Posts 
    .OrderByDescending(x => x.CreationDate) 
    .Take(postCount); 

最意見

var mostCommentedPosts = dbContext.Posts 
    .OrderByDescending(x => x.Comments.Count) 
    .Take(postCount); 

獲取X帖子最新的X帖子請評論,如果我得到了我的解釋是錯誤的。

+0

我想在OrderBy中放置更復雜的條件,請參閱編輯我的問題的一部分 –