Reputation: 22264
Here's my database schema:
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
)
Basically, I want to order the Posts by [VoteDirection == true] - [VoteDirection == false]
. So it 20 people votes "true", and 5 vote "false", the temporary score used for ordering would be 15.
Is there also a way to select a range of date using the DateOfVote parameter?
Here's what I've tried:
// 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);
}
I'm getting the error:
Error 2 Operator '-' cannot be applied to operands of type 'System.Collections.Generic.IEnumerable' and 'System.Collections.Generic.IEnumerable'
Which is understandable, they're both collections not actual integers. But it should illustrate what I'm trying to do. Any suggestions?
Upvotes: 1
Views: 108
Reputation: 102793
Just use Count
:
.OrderByDescending(x =>
x.Votes.Count(c => c.VoteDirection) - x.Votes.Count(c => !c.VoteDirection));
Upvotes: 4