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 に答える