1

这是我的数据库架构:

create table Post
(
    PostId int primary key identity,
    ImageUrl nvarchar(1024) not null,
    [Description] nvarchar(256) not null,
    UserId int foreign key references [User](UserId),
    DateOfPost datetime not null,
    Score int
)

create table Vote
(
    VoteId int primary key identity,
    UserId int foreign key references [User](UserId),
    PostId int foreign key references Post(PostId),
    VoteDirection bit not null,
    DateOfVote datetime not null
)

基本上,我想按[VoteDirection == true] - [VoteDirection == false]. 所以 20 人投“真”,5 人投“假”,用于排序的临时分数为 15。

还有一种方法可以使用 DateOfVote 参数选择日期范围吗?

这是我尝试过的:

    // www.foo.com/top/today
    public ActionResult Today()
    {
        var posts = postRepository.FindAllPosts().Where(x => x.DateOfPost == DateTime.Today)
                                                 .OrderByDescending(x => x.Votes.Where(c => c.VoteDirection == true) - x.Votes.Where(c => c.VoteDirection == false));
        return View(posts);
    }

我收到错误:

错误 2 运算符“-”不能应用于“System.Collections.Generic.IEnumerable”和“System.Collections.Generic.IEnumerable”类型的操作数

这是可以理解的,它们都是集合而不是实际的整数。但它应该说明我正在尝试做的事情。有什么建议么?

4

1 回答 1

4

只需使用Count

.OrderByDescending(x => 
    x.Votes.Count(c => c.VoteDirection) - x.Votes.Count(c => !c.VoteDirection));
于 2012-10-19T03:30:09.497 回答