-1

我想对我的帖子进行排序以显示按日期分组的查看次数最多的帖子,以便每天查看最多的帖子出现,然后是下一个,然后是下一个。

如何在 linq 中编写此查询?如果可能,基于方法。

4

1 回答 1

1
int postsPerDay = 5;

var topPostsByDate = Posts
    .GroupBy(post => post.Date)
    .Select(grp => new
        {
            Date = grp.Key,
            TopPosts = grp
                .OrderByDescending(post => post.Views)
                .Take(postsPerDay)
                .Select(post => new Post(post))
        })
    .OrderByDescending(x => x.Date);

编辑:从数据行构造一个新的 Post 对象。假设 Post 有一个构造函数接受DataRow; 改变以适应。

于 2012-09-17T19:53:06.630 回答